use std::collections::{HashMap, HashSet};
use std::fmt;
use std::path::{Component, Path};
use dcmnorm_core::header::{DataElement, Header, Length, Tag};
use dcmnorm_core::value::{DataSetSequence, PrimitiveValue, Value};
use dcmnorm_core::VR;
use dcmnorm_dictionary::uids;
use dcmnorm_encoding::transfer_syntax::TransferSyntaxIndex;
use dcmnorm_object::{DefaultDicomObject, FileMetaTableBuilder, InMemDicomObject, InMemElement};
use dcmnorm_transcode::TransferSyntaxRegistry;
use serde_json::Value as JsonValue;
use super::flat_json::read_flat_json_value;
use super::standard_json::read_standard_json_value;
use super::types::{DicomJsonError, DicomJsonFormat, ReadError, WriteError};
use super::volume_export::generate_uid;
pub const MEDIA_STORAGE_DIRECTORY_STORAGE: &str = "1.2.840.10008.1.3.10";
pub const MAX_FILE_ID_COMPONENTS: usize = 8;
pub const MAX_FILE_ID_COMPONENT_LEN: usize = 8;
pub const MAX_FILE_SET_ID_LEN: usize = 16;
const UTF8_CHARACTER_SET: &str = "ISO_IR 192";
const ITEM_TAG_BYTES: [u8; 4] = [0xFE, 0xFF, 0x00, 0xE0];
const FILE_SET_ID: Tag = Tag(0x0004, 0x1130);
const OFFSET_FIRST_ROOT: Tag = Tag(0x0004, 0x1200);
const OFFSET_LAST_ROOT: Tag = Tag(0x0004, 0x1202);
const FILE_SET_CONSISTENCY_FLAG: Tag = Tag(0x0004, 0x1212);
const DIRECTORY_RECORD_SEQUENCE: Tag = Tag(0x0004, 0x1220);
const OFFSET_NEXT: Tag = Tag(0x0004, 0x1400);
const RECORD_IN_USE_FLAG: Tag = Tag(0x0004, 0x1410);
const OFFSET_LOWER: Tag = Tag(0x0004, 0x1420);
const DIRECTORY_RECORD_TYPE: Tag = Tag(0x0004, 0x1430);
const REFERENCED_FILE_ID: Tag = Tag(0x0004, 0x1500);
const REFERENCED_SOP_CLASS_UID_IN_FILE: Tag = Tag(0x0004, 0x1510);
const REFERENCED_SOP_INSTANCE_UID_IN_FILE: Tag = Tag(0x0004, 0x1511);
const REFERENCED_TRANSFER_SYNTAX_UID_IN_FILE: Tag = Tag(0x0004, 0x1512);
const REFERENCED_RELATED_GENERAL_SOP_CLASS_UID_IN_FILE: Tag = Tag(0x0004, 0x151A);
const SPECIFIC_CHARACTER_SET: Tag = Tag(0x0008, 0x0005);
const INSTANCE_CREATION_DATE: Tag = Tag(0x0008, 0x0012);
const INSTANCE_CREATION_TIME: Tag = Tag(0x0008, 0x0013);
const SOP_CLASS_UID: Tag = Tag(0x0008, 0x0016);
const SOP_INSTANCE_UID: Tag = Tag(0x0008, 0x0018);
const RELATED_GENERAL_SOP_CLASS_UID: Tag = Tag(0x0008, 0x001A);
const STUDY_DATE: Tag = Tag(0x0008, 0x0020);
const SERIES_DATE: Tag = Tag(0x0008, 0x0021);
const ACQUISITION_DATE: Tag = Tag(0x0008, 0x0022);
const CONTENT_DATE: Tag = Tag(0x0008, 0x0023);
const STUDY_TIME: Tag = Tag(0x0008, 0x0030);
const SERIES_TIME: Tag = Tag(0x0008, 0x0031);
const ACQUISITION_TIME: Tag = Tag(0x0008, 0x0032);
const CONTENT_TIME: Tag = Tag(0x0008, 0x0033);
const MODALITY: Tag = Tag(0x0008, 0x0060);
const REFERENCED_IMAGE_SEQUENCE: Tag = Tag(0x0008, 0x1140);
const PATIENT_NAME: Tag = Tag(0x0010, 0x0010);
const PATIENT_ID: Tag = Tag(0x0010, 0x0020);
const PATIENT_BIRTH_DATE: Tag = Tag(0x0010, 0x0030);
const STUDY_INSTANCE_UID: Tag = Tag(0x0020, 0x000D);
const SERIES_INSTANCE_UID: Tag = Tag(0x0020, 0x000E);
const STUDY_ID: Tag = Tag(0x0020, 0x0010);
const SERIES_NUMBER: Tag = Tag(0x0020, 0x0011);
const INSTANCE_NUMBER: Tag = Tag(0x0020, 0x0013);
const IMAGE_POSITION_PATIENT: Tag = Tag(0x0020, 0x0032);
const IMAGE_ORIENTATION_PATIENT: Tag = Tag(0x0020, 0x0037);
const ROWS: Tag = Tag(0x0028, 0x0010);
const COLUMNS: Tag = Tag(0x0028, 0x0011);
const PIXEL_SPACING: Tag = Tag(0x0028, 0x0030);
const VERIFICATION_DATE_TIME: Tag = Tag(0x0040, 0xA030);
const VERIFYING_OBSERVER_SEQUENCE: Tag = Tag(0x0040, 0xA073);
const RELATIONSHIP_TYPE: Tag = Tag(0x0040, 0xA010);
const VERIFICATION_FLAG: Tag = Tag(0x0040, 0xA493);
const CONTENT_SEQUENCE: Tag = Tag(0x0040, 0xA730);
const SHARED_FUNCTIONAL_GROUPS_SEQUENCE: Tag = Tag(0x5200, 0x9229);
#[derive(Debug, Clone)]
pub struct DicomdirEntry {
pub file_id: Vec<String>,
pub dataset: InMemDicomObject,
pub transfer_syntax_uid: String,
pub sop_class_uid: Option<String>,
pub sop_instance_uid: Option<String>,
}
impl DicomdirEntry {
pub fn from_file_object(file_id: Vec<String>, object: &DefaultDicomObject) -> Self {
let meta = object.meta();
let non_empty = |s: &str| {
let s = trim_value(s);
(!s.is_empty()).then(|| s.to_owned())
};
let dataset: InMemDicomObject = (**object).clone();
let sop_class_uid = get_str(&dataset, SOP_CLASS_UID)
.or_else(|| non_empty(meta.media_storage_sop_class_uid()));
let sop_instance_uid = get_str(&dataset, SOP_INSTANCE_UID)
.or_else(|| non_empty(meta.media_storage_sop_instance_uid()));
DicomdirEntry {
file_id,
dataset,
transfer_syntax_uid: trim_value(meta.transfer_syntax()).to_owned(),
sop_class_uid,
sop_instance_uid,
}
}
pub fn from_file(file_id: Vec<String>, path: impl AsRef<Path>) -> Result<Self, DicomdirError> {
let path = path.as_ref();
let read_error = |source| DicomdirError::Read {
path: path.display().to_string(),
source,
};
let defer = |header: &dcmnorm_core::header::DataElementHeader| {
let tag = header.tag;
let payload = tag.group() >= 0x7FE0
|| tag == Tag(0x0042, 0x0011)
|| tag == Tag(0x5400, 0x1010)
|| (tag.group() & 0xFF01 == 0x6000 && tag.element() == 0x3000);
let large_binary = matches!(
header.vr,
VR::OB | VR::OW | VR::OF | VR::OD | VR::OL | VR::OV
) && header.len.get().is_some_and(|len| len > 1024);
payload || large_binary
};
let deferred = dcmnorm_object::OpenFileOptions::new()
.read_preamble(dcmnorm_object::ReadPreamble::Always)
.open_file_deferring(path, &defer, true);
let object = match deferred {
Ok(Some((object, _locations))) => object,
Ok(None) => super::io::read_dicom_file(path).map_err(read_error)?,
Err(source) => return Err(read_error(source)),
};
Ok(Self::from_file_object(file_id, &object))
}
pub fn from_json(
file_id: Vec<String>,
json: &str,
format: DicomJsonFormat,
transfer_syntax_uid: Option<&str>,
) -> Result<Self, DicomdirError> {
let mut value: JsonValue =
serde_json::from_str(json).map_err(|e| DicomdirError::Json(DicomJsonError::from(e)))?;
strip_bulk_data_uris(&mut value);
let object = match format {
DicomJsonFormat::Flat => read_flat_json_value(&value, None),
DicomJsonFormat::Standard => read_standard_json_value(&value, None),
}
.map_err(DicomdirError::Json)?;
let mut entry = Self::from_file_object(file_id, &object);
if let Some(ts) = transfer_syntax_uid
.map(trim_value)
.filter(|ts| !ts.is_empty())
{
entry.transfer_syntax_uid = ts.to_owned();
}
Ok(entry)
}
}
#[derive(Debug, Clone, Default)]
pub struct DicomdirOptions {
pub file_set_id: String,
pub sop_instance_uid: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DicomdirSynthesized {
pub record_type: String,
pub entity: String,
pub attribute: String,
pub value: String,
pub source: Option<String>,
}
#[derive(Debug, Clone, Default)]
pub struct DicomdirReport {
pub record_count: usize,
pub record_types: Vec<(String, usize)>,
pub synthesized: Vec<DicomdirSynthesized>,
pub warnings: Vec<String>,
}
#[derive(Debug)]
pub enum DicomdirError {
NoEntries,
InvalidFileId {
file_id: String,
reason: String,
},
InvalidFileSetId {
file_set_id: String,
reason: String,
},
DuplicateFileId(String),
DuplicateSopInstance {
sop_instance_uid: String,
file_ids: (String, String),
},
MissingAttribute {
file_id: String,
attribute: &'static str,
},
NoRecordType {
file_id: String,
sop_class_uid: String,
},
PatientConflict {
patient_id: String,
names: (String, String),
},
StudyConflict {
study_instance_uid: String,
},
SeriesConflict {
series_instance_uid: String,
},
Read {
path: String,
source: ReadError,
},
Json(DicomJsonError),
Write(WriteError),
Io(std::io::Error),
Tree(String),
}
impl fmt::Display for DicomdirError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NoEntries => write!(f, "a DICOMDIR needs at least one referenced file (PS3.11 D.3.3)"),
Self::InvalidFileId { file_id, reason } => write!(f, "invalid File ID {file_id:?}: {reason}"),
Self::InvalidFileSetId { file_set_id, reason } => {
write!(f, "invalid File-set ID {file_set_id:?}: {reason}")
}
Self::DuplicateFileId(id) => write!(f, "File ID {id} is referenced twice"),
Self::DuplicateSopInstance { sop_instance_uid, file_ids } => write!(
f,
"SOP Instance UID {sop_instance_uid} is in two files ({} and {})",
file_ids.0, file_ids.1
),
Self::MissingAttribute { file_id, attribute } => {
write!(f, "{file_id}: {attribute} is missing or empty, and a DICOMDIR can't be built without it")
}
Self::NoRecordType { file_id, sop_class_uid } => write!(
f,
"{file_id}: SOP Class {sop_class_uid} has no Directory Record Type (PS3.3 F.5) and is not an image"
),
Self::PatientConflict { patient_id, names } => write!(
f,
"Patient ID {patient_id:?} is used by two different patients ({:?} and {:?}); a File-set needs one Patient ID per patient (PS3.3 F.5.1)",
names.0, names.1
),
Self::StudyConflict { study_instance_uid } => write!(
f,
"the instances of study {study_instance_uid} name different patients (Patient ID, or name and birth date when there is no ID)"
),
Self::SeriesConflict { series_instance_uid } => {
write!(f, "Series Instance UID {series_instance_uid} appears in two different studies")
}
Self::Read { path, source } => write!(f, "{path}: {source}"),
Self::Json(e) => write!(f, "invalid DICOM JSON: {e}"),
Self::Write(e) => write!(f, "failed to write DICOMDIR: {e}"),
Self::Io(e) => write!(f, "failed to write DICOMDIR: {e}"),
Self::Tree(message) => f.write_str(message),
}
}
}
impl std::error::Error for DicomdirError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Read { source, .. } => Some(source),
Self::Json(e) => Some(e),
Self::Write(e) => Some(e),
Self::Io(e) => Some(e),
_ => None,
}
}
}
pub fn validate_file_id<S: AsRef<str>>(components: &[S]) -> Result<(), DicomdirError> {
let joined = || {
components
.iter()
.map(|c| c.as_ref())
.collect::<Vec<_>>()
.join("\\")
};
let invalid = |reason: String| DicomdirError::InvalidFileId {
file_id: joined(),
reason,
};
if components.is_empty() {
return Err(invalid("a File ID needs at least one component".into()));
}
if components.len() > MAX_FILE_ID_COMPONENTS {
return Err(invalid(format!(
"{} components, at most {MAX_FILE_ID_COMPONENTS} allowed (PS3.10 8.2)",
components.len()
)));
}
for component in components {
let component = component.as_ref();
if component.is_empty() || component.len() > MAX_FILE_ID_COMPONENT_LEN {
return Err(invalid(format!(
"component {component:?} must be 1-{MAX_FILE_ID_COMPONENT_LEN} characters (PS3.10 8.2)"
)));
}
if let Some(c) = component.chars().find(|c| !is_file_id_char(*c)) {
return Err(invalid(format!(
"component {component:?} has {c:?}; only A-Z, 0-9 and _ are allowed (PS3.10 8.5), so no lower case and no extension"
)));
}
}
if components.len() == 1 && components[0].as_ref() == "DICOMDIR" {
return Err(invalid(
"DICOMDIR is the reserved File ID of the directory itself (PS3.10 8.6)".into(),
));
}
Ok(())
}
pub fn validate_file_set_id(file_set_id: &str) -> Result<(), DicomdirError> {
let invalid = |reason: String| DicomdirError::InvalidFileSetId {
file_set_id: file_set_id.to_owned(),
reason,
};
if file_set_id.len() > MAX_FILE_SET_ID_LEN {
return Err(invalid(format!("at most {MAX_FILE_SET_ID_LEN} characters")));
}
if let Some(c) = file_set_id.chars().find(|c| !is_file_id_char(*c)) {
return Err(invalid(format!(
"{c:?} is not allowed; only A-Z, 0-9 and _ (PS3.10 8.5)"
)));
}
Ok(())
}
fn is_file_id_char(c: char) -> bool {
c.is_ascii_uppercase() || c.is_ascii_digit() || c == '_'
}
pub fn file_id_from_relative_path(path: &Path) -> Result<Vec<String>, DicomdirError> {
let mut components = Vec::new();
for component in path.components() {
match component {
Component::Normal(part) => components.push(part.to_string_lossy().into_owned()),
Component::CurDir => {}
_ => {
return Err(DicomdirError::InvalidFileId {
file_id: path.display().to_string(),
reason: "must be a path below the File-set root".into(),
})
}
}
}
validate_file_id(&components)?;
Ok(components)
}
pub fn directory_record_type_for_sop_class(
sop_class_uid: &str,
modality: Option<&str>,
) -> Option<&'static str> {
let uid = trim_value(sop_class_uid);
if let Some(record_type) = NON_IMAGE_SOP_CLASSES
.iter()
.find(|(u, _)| *u == uid)
.map(|(_, t)| *t)
{
return Some(record_type);
}
match modality.map(trim_value) {
Some("PLAN") => Some("PLAN"),
Some("RTINTENT" | "RTSEGANN" | "RTRAD" | "RTRECORD")
if uid.starts_with("1.2.840.10008.5.1.4.1.1.481.") =>
{
Some("RADIOTHERAPY")
}
_ => None,
}
}
pub fn build_dicomdir(
entries: &[DicomdirEntry],
options: &DicomdirOptions,
) -> Result<(Vec<u8>, DicomdirReport), DicomdirError> {
let file_set_id = trim_value(&options.file_set_id).to_owned();
validate_file_set_id(&file_set_id)?;
if entries.is_empty() {
return Err(DicomdirError::NoEntries);
}
let mut report = DicomdirReport::default();
let tree = build_tree(entries, &mut report)?;
let bytes = encode_dicomdir(&tree, &file_set_id, options.sop_instance_uid.as_deref())?;
report.record_count = tree.records.len();
for record in &tree.records {
match report
.record_types
.iter_mut()
.find(|(t, _)| t == record.record_type)
{
Some((_, n)) => *n += 1,
None => report.record_types.push((record.record_type.to_owned(), 1)),
}
}
Ok((bytes, report))
}
pub fn write_dicomdir(
entries: &[DicomdirEntry],
path: impl AsRef<Path>,
options: &DicomdirOptions,
) -> Result<DicomdirReport, DicomdirError> {
let (bytes, report) = build_dicomdir(entries, options)?;
std::fs::write(path.as_ref(), bytes).map_err(DicomdirError::Io)?;
Ok(report)
}
pub fn collect_dicomdir_entries(
root: &Path,
) -> Result<(Vec<DicomdirEntry>, Vec<String>), DicomdirError> {
if !root.is_dir() {
return Err(DicomdirError::Tree(format!(
"{} is not a directory",
root.display()
)));
}
let mut files = Vec::new();
let mut stack = vec![root.to_path_buf()];
while let Some(dir) = stack.pop() {
for entry in std::fs::read_dir(&dir).map_err(DicomdirError::Io)? {
let entry = entry.map_err(DicomdirError::Io)?;
let file_type = entry.file_type().map_err(DicomdirError::Io)?;
let path = entry.path();
if file_type.is_dir() {
stack.push(path);
} else if file_type.is_file() || (file_type.is_symlink() && path.is_file()) {
files.push(path);
}
}
}
files.sort();
let mut entries = Vec::new();
let mut skipped = Vec::new();
for path in files {
let relative = path
.strip_prefix(root)
.expect("walked path is below root")
.to_path_buf();
if relative == Path::new("DICOMDIR") {
continue; }
if !has_dicm_prefix(&path) {
skipped.push(relative.display().to_string());
continue;
}
let file_id = file_id_from_relative_path(&relative)?;
entries.push(DicomdirEntry::from_file(file_id, &path)?);
}
Ok((entries, skipped))
}
fn has_dicm_prefix(path: &Path) -> bool {
use std::io::Read;
let mut head = [0u8; 132];
std::fs::File::open(path)
.and_then(|mut f| f.read_exact(&mut head))
.map(|_| &head[128..132] == b"DICM")
.unwrap_or(false)
}
const NON_IMAGE_SOP_CLASSES: &[(&str, &str)] = &[
("1.2.840.10008.5.1.4.1.1.481.2", "RT DOSE"),
("1.2.840.10008.5.1.4.1.1.481.3", "RT STRUCTURE SET"),
("1.2.840.10008.5.1.4.1.1.481.5", "RT PLAN"),
("1.2.840.10008.5.1.4.1.1.481.8", "RT PLAN"),
("1.2.840.10008.5.1.4.1.1.481.4", "RT TREAT RECORD"),
("1.2.840.10008.5.1.4.1.1.481.6", "RT TREAT RECORD"),
("1.2.840.10008.5.1.4.1.1.481.7", "RT TREAT RECORD"),
("1.2.840.10008.5.1.4.1.1.481.9", "RT TREAT RECORD"),
("1.2.840.10008.5.1.4.1.1.11.1", "PRESENTATION"),
("1.2.840.10008.5.1.4.1.1.11.2", "PRESENTATION"),
("1.2.840.10008.5.1.4.1.1.11.3", "PRESENTATION"),
("1.2.840.10008.5.1.4.1.1.11.4", "PRESENTATION"),
("1.2.840.10008.5.1.4.1.1.11.5", "PRESENTATION"),
("1.2.840.10008.5.1.4.1.1.11.6", "PRESENTATION"),
("1.2.840.10008.5.1.4.1.1.11.7", "PRESENTATION"),
("1.2.840.10008.5.1.4.1.1.11.8", "PRESENTATION"),
("1.2.840.10008.5.1.4.1.1.11.9", "PRESENTATION"),
("1.2.840.10008.5.1.4.1.1.11.10", "PRESENTATION"),
("1.2.840.10008.5.1.4.1.1.11.11", "PRESENTATION"),
("1.2.840.10008.5.1.4.1.1.11.12", "PRESENTATION"),
("1.2.840.10008.5.1.4.1.1.131", "PRESENTATION"),
("1.2.840.10008.5.1.4.1.1.9.1.1", "WAVEFORM"),
("1.2.840.10008.5.1.4.1.1.9.1.2", "WAVEFORM"),
("1.2.840.10008.5.1.4.1.1.9.1.3", "WAVEFORM"),
("1.2.840.10008.5.1.4.1.1.9.1.4", "WAVEFORM"),
("1.2.840.10008.5.1.4.1.1.9.2.1", "WAVEFORM"),
("1.2.840.10008.5.1.4.1.1.9.3.1", "WAVEFORM"),
("1.2.840.10008.5.1.4.1.1.9.4.1", "WAVEFORM"),
("1.2.840.10008.5.1.4.1.1.9.4.2", "WAVEFORM"),
("1.2.840.10008.5.1.4.1.1.9.5.1", "WAVEFORM"),
("1.2.840.10008.5.1.4.1.1.9.6.1", "WAVEFORM"),
("1.2.840.10008.5.1.4.1.1.9.6.2", "WAVEFORM"),
("1.2.840.10008.5.1.4.1.1.9.7.1", "WAVEFORM"),
("1.2.840.10008.5.1.4.1.1.9.7.2", "WAVEFORM"),
("1.2.840.10008.5.1.4.1.1.9.7.3", "WAVEFORM"),
("1.2.840.10008.5.1.4.1.1.9.7.4", "WAVEFORM"),
("1.2.840.10008.5.1.4.1.1.9.8.1", "WAVEFORM"),
("1.2.840.10008.5.1.4.1.1.601.5", "WAVEFORM"),
("1.2.840.10008.5.1.4.1.1.88.11", "SR DOCUMENT"),
("1.2.840.10008.5.1.4.1.1.88.22", "SR DOCUMENT"),
("1.2.840.10008.5.1.4.1.1.88.33", "SR DOCUMENT"),
("1.2.840.10008.5.1.4.1.1.88.34", "SR DOCUMENT"),
("1.2.840.10008.5.1.4.1.1.88.35", "SR DOCUMENT"),
("1.2.840.10008.5.1.4.1.1.88.40", "SR DOCUMENT"),
("1.2.840.10008.5.1.4.1.1.88.50", "SR DOCUMENT"),
("1.2.840.10008.5.1.4.1.1.88.65", "SR DOCUMENT"),
("1.2.840.10008.5.1.4.1.1.88.67", "SR DOCUMENT"),
("1.2.840.10008.5.1.4.1.1.88.68", "SR DOCUMENT"),
("1.2.840.10008.5.1.4.1.1.88.69", "SR DOCUMENT"),
("1.2.840.10008.5.1.4.1.1.88.70", "SR DOCUMENT"),
("1.2.840.10008.5.1.4.1.1.88.71", "SR DOCUMENT"),
("1.2.840.10008.5.1.4.1.1.88.72", "SR DOCUMENT"),
("1.2.840.10008.5.1.4.1.1.88.73", "SR DOCUMENT"),
("1.2.840.10008.5.1.4.1.1.88.74", "SR DOCUMENT"),
("1.2.840.10008.5.1.4.1.1.88.75", "SR DOCUMENT"),
("1.2.840.10008.5.1.4.1.1.88.76", "SR DOCUMENT"),
("1.2.840.10008.5.1.4.1.1.88.77", "SR DOCUMENT"),
("1.2.840.10008.5.1.4.1.1.78.6", "SR DOCUMENT"),
("1.2.840.10008.5.1.4.1.1.79.1", "SR DOCUMENT"),
("1.2.840.10008.5.1.4.1.1.88.1", "SR DOCUMENT"),
("1.2.840.10008.5.1.4.1.1.88.2", "SR DOCUMENT"),
("1.2.840.10008.5.1.4.1.1.88.3", "SR DOCUMENT"),
("1.2.840.10008.5.1.4.1.1.88.4", "SR DOCUMENT"),
("1.2.840.10008.5.1.4.1.1.88.59", "KEY OBJECT DOC"),
("1.2.840.10008.5.1.4.1.1.4.2", "SPECTROSCOPY"),
("1.2.840.10008.5.1.4.1.1.66", "RAW DATA"),
("1.2.840.10008.5.1.4.1.1.66.1", "REGISTRATION"),
("1.2.840.10008.5.1.4.1.1.66.3", "REGISTRATION"),
("1.2.840.10008.5.1.4.1.1.66.2", "FIDUCIAL"),
("1.2.840.10008.5.1.4.38.1", "HANGING PROTOCOL"),
("1.2.840.10008.5.1.4.1.1.104.1", "ENCAP DOC"),
("1.2.840.10008.5.1.4.1.1.104.2", "ENCAP DOC"),
("1.2.840.10008.5.1.4.1.1.104.3", "ENCAP DOC"),
("1.2.840.10008.5.1.4.1.1.104.4", "ENCAP DOC"),
("1.2.840.10008.5.1.4.1.1.104.5", "ENCAP DOC"),
("1.2.840.10008.5.1.4.1.1.67", "VALUE MAP"),
("1.2.840.10008.5.1.4.1.1.77.1.5.3", "STEREOMETRIC"),
("1.2.840.10008.5.1.4.39.1", "PALETTE"),
("1.2.840.10008.5.1.4.43.1", "IMPLANT"),
("1.2.840.10008.5.1.4.44.1", "IMPLANT ASSY"),
("1.2.840.10008.5.1.4.45.1", "IMPLANT GROUP"),
("1.2.840.10008.5.1.4.34.7", "PLAN"),
("1.2.840.10008.5.1.4.34.10", "PLAN"),
("1.2.840.10008.5.1.4.1.1.78.1", "MEASUREMENT"),
("1.2.840.10008.5.1.4.1.1.78.2", "MEASUREMENT"),
("1.2.840.10008.5.1.4.1.1.78.3", "MEASUREMENT"),
("1.2.840.10008.5.1.4.1.1.78.4", "MEASUREMENT"),
("1.2.840.10008.5.1.4.1.1.78.5", "MEASUREMENT"),
("1.2.840.10008.5.1.4.1.1.78.7", "MEASUREMENT"),
("1.2.840.10008.5.1.4.1.1.78.8", "MEASUREMENT"),
("1.2.840.10008.5.1.4.1.1.80.1", "MEASUREMENT"),
("1.2.840.10008.5.1.4.1.1.66.5", "SURFACE"),
("1.2.840.10008.5.1.4.1.1.68.1", "SURFACE SCAN"),
("1.2.840.10008.5.1.4.1.1.68.2", "SURFACE SCAN"),
("1.2.840.10008.5.1.4.1.1.66.6", "TRACT"),
("1.2.840.10008.5.1.4.1.1.90.1", "ASSESSMENT"),
("1.2.840.10008.5.1.4.1.1.481.10", "RADIOTHERAPY"),
("1.2.840.10008.5.1.4.1.1.481.11", "RADIOTHERAPY"),
("1.2.840.10008.5.1.4.1.1.481.12", "RADIOTHERAPY"),
("1.2.840.10008.5.1.4.1.1.481.13", "RADIOTHERAPY"),
("1.2.840.10008.5.1.4.1.1.481.14", "RADIOTHERAPY"),
("1.2.840.10008.5.1.4.1.1.481.15", "RADIOTHERAPY"),
("1.2.840.10008.5.1.4.1.1.481.16", "RADIOTHERAPY"),
("1.2.840.10008.5.1.4.1.1.481.17", "RADIOTHERAPY"),
("1.2.840.10008.5.1.4.1.1.481.18", "RADIOTHERAPY"),
("1.2.840.10008.5.1.4.1.1.481.19", "RADIOTHERAPY"),
("1.2.840.10008.5.1.4.1.1.481.20", "RADIOTHERAPY"),
("1.2.840.10008.5.1.4.1.1.481.21", "RADIOTHERAPY"),
("1.2.840.10008.5.1.4.1.1.481.22", "RADIOTHERAPY"),
("1.2.840.10008.5.1.4.1.1.481.23", "RADIOTHERAPY"),
("1.2.840.10008.5.1.4.1.1.481.24", "RADIOTHERAPY"),
("1.2.840.10008.5.1.4.1.1.481.25", "RADIOTHERAPY"),
("1.2.840.10008.5.1.4.1.1.91.1", "ANNOTATION"),
("1.2.840.10008.5.1.4.1.1.201.1", "INVENTORY"),
("1.2.840.10008.5.1.4.1.1.9.100.1", "WF PRESENTATION"),
("1.2.840.10008.5.1.4.1.1.9.100.2", "WF PRESENTATION"),
];
const ROOT_LEVEL_RECORD_TYPES: &[&str] = &[
"HANGING PROTOCOL",
"PALETTE",
"IMPLANT",
"IMPLANT ASSY",
"IMPLANT GROUP",
"INVENTORY",
];
fn record_type_for_entry(
entry: &DicomdirEntry,
sop_class_uid: &str,
file_id: &str,
) -> Result<&'static str, DicomdirError> {
let modality = get_str(&entry.dataset, MODALITY);
if let Some(record_type) =
directory_record_type_for_sop_class(sop_class_uid, modality.as_deref())
{
return Ok(record_type);
}
let has_pixels = entry.dataset.get(ROWS).is_some() && entry.dataset.get(COLUMNS).is_some();
if has_pixels {
return Ok("IMAGE");
}
Err(DicomdirError::NoRecordType {
file_id: file_id.to_owned(),
sop_class_uid: sop_class_uid.to_owned(),
})
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum KeyType {
One,
Two,
IfPresent,
}
#[derive(Clone, Copy, Debug)]
struct Key {
tag: Tag,
vr: VR,
keyword: &'static str,
kind: KeyType,
fallbacks: &'static [Tag],
}
const fn key(group: u16, element: u16, vr: VR, keyword: &'static str, kind: KeyType) -> Key {
Key {
tag: Tag(group, element),
vr,
keyword,
kind,
fallbacks: &[],
}
}
const fn key_or(
group: u16,
element: u16,
vr: VR,
keyword: &'static str,
fallbacks: &'static [Tag],
) -> Key {
Key {
tag: Tag(group, element),
vr,
keyword,
kind: KeyType::One,
fallbacks,
}
}
use KeyType::{IfPresent, One, Two};
const DATE_FALLBACKS_FOR_STUDY: &[Tag] = &[
SERIES_DATE,
ACQUISITION_DATE,
CONTENT_DATE,
INSTANCE_CREATION_DATE,
];
const TIME_FALLBACKS_FOR_STUDY: &[Tag] = &[
SERIES_TIME,
ACQUISITION_TIME,
CONTENT_TIME,
INSTANCE_CREATION_TIME,
];
const CONTENT_DATE_FALLBACKS: &[Tag] = &[INSTANCE_CREATION_DATE, SERIES_DATE, STUDY_DATE];
const CONTENT_TIME_FALLBACKS: &[Tag] = &[INSTANCE_CREATION_TIME, SERIES_TIME, STUDY_TIME];
const CONTENT_DATE_KEY: Key = key_or(
0x0008,
0x0023,
VR::DA,
"ContentDate",
CONTENT_DATE_FALLBACKS,
);
const CONTENT_TIME_KEY: Key = key_or(
0x0008,
0x0033,
VR::TM,
"ContentTime",
CONTENT_TIME_FALLBACKS,
);
const INSTANCE_NUMBER_KEY: Key = key(0x0020, 0x0013, VR::IS, "InstanceNumber", One);
const PATIENT_ID_KEY: Key = key(0x0010, 0x0020, VR::LO, "PatientID", One);
const STUDY_ID_KEY: Key = key(0x0020, 0x0010, VR::SH, "StudyID", One);
const MODALITY_KEY: Key = key(0x0008, 0x0060, VR::CS, "Modality", One);
const SERIES_NUMBER_KEY: Key = key(0x0020, 0x0011, VR::IS, "SeriesNumber", One);
const PATIENT_KEYS: &[Key] = &[
key(0x0010, 0x0010, VR::PN, "PatientName", Two),
PATIENT_ID_KEY,
key(0x0010, 0x0030, VR::DA, "PatientBirthDate", IfPresent),
key(0x0010, 0x0040, VR::CS, "PatientSex", IfPresent),
];
const STUDY_KEYS: &[Key] = &[
key_or(
0x0008,
0x0020,
VR::DA,
"StudyDate",
DATE_FALLBACKS_FOR_STUDY,
),
key_or(
0x0008,
0x0030,
VR::TM,
"StudyTime",
TIME_FALLBACKS_FOR_STUDY,
),
key(0x0008, 0x0050, VR::SH, "AccessionNumber", Two),
key(0x0008, 0x1030, VR::LO, "StudyDescription", Two),
key(0x0020, 0x000D, VR::UI, "StudyInstanceUID", One),
STUDY_ID_KEY,
];
const SERIES_KEYS: &[Key] = &[
MODALITY_KEY,
key(0x0008, 0x0080, VR::LO, "InstitutionName", IfPresent),
key(0x0008, 0x0081, VR::ST, "InstitutionAddress", IfPresent),
key(0x0008, 0x103E, VR::LO, "SeriesDescription", IfPresent),
key(0x0008, 0x1050, VR::PN, "PerformingPhysicianName", IfPresent),
key(0x0020, 0x000E, VR::UI, "SeriesInstanceUID", One),
SERIES_NUMBER_KEY,
];
const IMAGE_KEYS: &[Key] = &[
key(0x0008, 0x0008, VR::CS, "ImageType", IfPresent),
key(0x0008, 0x002A, VR::DT, "AcquisitionDateTime", IfPresent),
key(0x0008, 0x1140, VR::SQ, "ReferencedImageSequence", IfPresent),
key(
0x0018,
0x1800,
VR::CS,
"AcquisitionTimeSynchronized",
IfPresent,
),
INSTANCE_NUMBER_KEY,
key(0x0020, 0x0032, VR::DS, "ImagePositionPatient", IfPresent),
key(0x0020, 0x0037, VR::DS, "ImageOrientationPatient", IfPresent),
key(0x0020, 0x0052, VR::UI, "FrameOfReferenceUID", IfPresent),
key(
0x0020,
0x0200,
VR::UI,
"SynchronizationFrameOfReferenceUID",
IfPresent,
),
key(0x0028, 0x0008, VR::IS, "NumberOfFrames", IfPresent),
key(0x0028, 0x0010, VR::US, "Rows", IfPresent),
key(0x0028, 0x0011, VR::US, "Columns", IfPresent),
key(0x0028, 0x0030, VR::DS, "PixelSpacing", IfPresent),
key(
0x0028,
0x2112,
VR::DS,
"LossyImageCompressionRatio",
IfPresent,
),
key(0x0050, 0x0004, VR::CS, "CalibrationImage", IfPresent),
];
const RT_DOSE_KEYS: &[Key] = &[
INSTANCE_NUMBER_KEY,
key(0x3004, 0x0006, VR::LO, "DoseComment", IfPresent),
key(0x3004, 0x000A, VR::CS, "DoseSummationType", One),
];
const RT_STRUCTURE_SET_KEYS: &[Key] = &[
INSTANCE_NUMBER_KEY,
key(0x3006, 0x0002, VR::SH, "StructureSetLabel", One),
key(0x3006, 0x0008, VR::DA, "StructureSetDate", Two),
key(0x3006, 0x0009, VR::TM, "StructureSetTime", Two),
];
const RT_PLAN_KEYS: &[Key] = &[
INSTANCE_NUMBER_KEY,
key(0x300A, 0x0002, VR::SH, "RTPlanLabel", One),
key(0x300A, 0x0006, VR::DA, "RTPlanDate", Two),
key(0x300A, 0x0007, VR::TM, "RTPlanTime", Two),
];
const RT_TREAT_RECORD_KEYS: &[Key] = &[
INSTANCE_NUMBER_KEY,
key(0x3008, 0x0250, VR::DA, "TreatmentDate", Two),
key(0x3008, 0x0251, VR::TM, "TreatmentTime", Two),
];
const PRESENTATION_KEYS: &[Key] = &[
key(
0x0008,
0x1115,
VR::SQ,
"ReferencedSeriesSequence",
IfPresent,
),
INSTANCE_NUMBER_KEY_IF_PRESENT,
key(0x0070, 0x0080, VR::CS, "ContentLabel", IfPresent),
key(0x0070, 0x0081, VR::LO, "ContentDescription", IfPresent),
key_or(
0x0070,
0x0082,
VR::DA,
"PresentationCreationDate",
CONTENT_DATE_FALLBACKS,
),
key_or(
0x0070,
0x0083,
VR::TM,
"PresentationCreationTime",
CONTENT_TIME_FALLBACKS,
),
key(0x0070, 0x0084, VR::PN, "ContentCreatorName", IfPresent),
key(0x0070, 0x0402, VR::SQ, "BlendingSequence", IfPresent),
];
const INSTANCE_NUMBER_KEY_IF_PRESENT: Key =
key(0x0020, 0x0013, VR::IS, "InstanceNumber", IfPresent);
const WAVEFORM_KEYS: &[Key] = &[CONTENT_DATE_KEY, CONTENT_TIME_KEY, INSTANCE_NUMBER_KEY];
const SR_DOCUMENT_KEYS: &[Key] = &[
CONTENT_DATE_KEY,
CONTENT_TIME_KEY,
INSTANCE_NUMBER_KEY,
key(0x0040, 0xA043, VR::SQ, "ConceptNameCodeSequence", One),
key(0x0040, 0xA491, VR::CS, "CompletionFlag", One),
key(0x0040, 0xA493, VR::CS, "VerificationFlag", One),
];
const KEY_OBJECT_DOC_KEYS: &[Key] = &[
CONTENT_DATE_KEY,
CONTENT_TIME_KEY,
INSTANCE_NUMBER_KEY,
key(0x0040, 0xA043, VR::SQ, "ConceptNameCodeSequence", One),
];
const SPECTROSCOPY_KEYS: &[Key] = &[
key(0x0008, 0x0008, VR::CS, "ImageType", One),
CONTENT_DATE_KEY,
CONTENT_TIME_KEY,
key(0x0008, 0x002A, VR::DT, "AcquisitionDateTime", IfPresent),
key(0x0008, 0x1140, VR::SQ, "ReferencedImageSequence", IfPresent),
key(
0x0008,
0x9092,
VR::SQ,
"ReferencedImageEvidenceSequence",
IfPresent,
),
key(
0x0018,
0x1800,
VR::CS,
"AcquisitionTimeSynchronized",
IfPresent,
),
INSTANCE_NUMBER_KEY,
key(0x0020, 0x0032, VR::DS, "ImagePositionPatient", IfPresent),
key(0x0020, 0x0037, VR::DS, "ImageOrientationPatient", IfPresent),
key(0x0020, 0x0052, VR::UI, "FrameOfReferenceUID", IfPresent),
key(
0x0020,
0x0200,
VR::UI,
"SynchronizationFrameOfReferenceUID",
IfPresent,
),
key(0x0028, 0x0008, VR::IS, "NumberOfFrames", One),
key(0x0028, 0x0010, VR::US, "Rows", One),
key(0x0028, 0x0011, VR::US, "Columns", One),
key(0x0028, 0x0030, VR::DS, "PixelSpacing", IfPresent),
key(0x0028, 0x9001, VR::UL, "DataPointRows", One),
key(0x0028, 0x9002, VR::UL, "DataPointColumns", One),
];
const RAW_DATA_KEYS: &[Key] = &[
CONTENT_DATE_KEY,
CONTENT_TIME_KEY,
key(0x0020, 0x0013, VR::IS, "InstanceNumber", Two),
];
const CONTENT_DATE_TIME_KEYS: &[Key] = &[CONTENT_DATE_KEY, CONTENT_TIME_KEY];
const HANGING_PROTOCOL_KEYS: &[Key] = &[
key(0x0072, 0x0002, VR::SH, "HangingProtocolName", One),
key(0x0072, 0x0004, VR::LO, "HangingProtocolDescription", One),
key(0x0072, 0x0006, VR::CS, "HangingProtocolLevel", One),
key(0x0072, 0x0008, VR::LO, "HangingProtocolCreator", One),
key(
0x0072,
0x000A,
VR::DT,
"HangingProtocolCreationDateTime",
One,
),
key(
0x0072,
0x000C,
VR::SQ,
"HangingProtocolDefinitionSequence",
One,
),
key(
0x0072,
0x000E,
VR::SQ,
"HangingProtocolUserIdentificationCodeSequence",
Two,
),
key(0x0072, 0x0014, VR::US, "NumberOfPriorsReferenced", One),
];
const ENCAP_DOC_KEYS: &[Key] = &[
key(0x0008, 0x0023, VR::DA, "ContentDate", Two),
key(0x0008, 0x0033, VR::TM, "ContentTime", Two),
INSTANCE_NUMBER_KEY,
key(0x0040, 0xA043, VR::SQ, "ConceptNameCodeSequence", Two),
key(0x0040, 0xE001, VR::ST, "HL7InstanceIdentifier", IfPresent),
key(0x0042, 0x0010, VR::ST, "DocumentTitle", Two),
key(
0x0042,
0x0012,
VR::LO,
"MIMETypeOfEncapsulatedDocument",
One,
),
];
const PALETTE_KEYS: &[Key] = &[
key(0x0070, 0x0080, VR::CS, "ContentLabel", One),
key(0x0070, 0x0081, VR::LO, "ContentDescription", Two),
];
const IMPLANT_KEYS: &[Key] = &[
key(0x0008, 0x0070, VR::LO, "Manufacturer", One),
key(0x0022, 0x1095, VR::LO, "ImplantName", One),
key(0x0022, 0x1097, VR::LO, "ImplantPartNumber", One),
key(0x0068, 0x6210, VR::LO, "ImplantSize", IfPresent),
];
const IMPLANT_ASSY_KEYS: &[Key] = &[
key(0x0008, 0x0070, VR::LO, "Manufacturer", One),
key(0x0076, 0x0001, VR::LO, "ImplantAssemblyTemplateName", One),
key(0x0076, 0x0020, VR::SQ, "ProcedureTypeCodeSequence", One),
];
const IMPLANT_GROUP_KEYS: &[Key] = &[
key(0x0078, 0x0001, VR::LO, "ImplantTemplateGroupName", One),
key(0x0078, 0x0020, VR::LO, "ImplantTemplateGroupIssuer", One),
];
const ASSESSMENT_KEYS: &[Key] = &[
key(0x0008, 0x0012, VR::DA, "InstanceCreationDate", One),
key(0x0008, 0x0013, VR::TM, "InstanceCreationTime", Two),
INSTANCE_NUMBER_KEY,
];
const RADIOTHERAPY_KEYS: &[Key] = &[
INSTANCE_NUMBER_KEY,
key(0x0070, 0x0081, VR::LO, "ContentDescription", Two),
key(0x0070, 0x0084, VR::PN, "ContentCreatorName", Two),
key(0x3010, 0x0033, VR::SH, "UserContentLabel", IfPresent),
key(0x3010, 0x0034, VR::LO, "UserContentLongLabel", IfPresent),
];
const INVENTORY_KEYS: &[Key] = &[
CONTENT_DATE_KEY,
CONTENT_TIME_KEY,
key(0x0008, 0x0401, VR::CS, "InventoryPurpose", Two),
key(0x0008, 0x0403, VR::CS, "InventoryLevel", One),
key(0x0008, 0x0426, VR::CS, "InventoryCompletionStatus", One),
key(0x0008, 0x0428, VR::UL, "TotalNumberOfStudyRecords", One),
];
const WF_PRESENTATION_KEYS: &[Key] = &[
key(
0x0008,
0x1115,
VR::SQ,
"ReferencedSeriesSequence",
IfPresent,
),
key_or(
0x0070,
0x0082,
VR::DA,
"PresentationCreationDate",
CONTENT_DATE_FALLBACKS,
),
key_or(
0x0070,
0x0083,
VR::TM,
"PresentationCreationTime",
CONTENT_TIME_FALLBACKS,
),
];
const NO_KEYS: &[Key] = &[];
fn leaf_keys(record_type: &str) -> &'static [Key] {
match record_type {
"IMAGE" => IMAGE_KEYS,
"RT DOSE" => RT_DOSE_KEYS,
"RT STRUCTURE SET" => RT_STRUCTURE_SET_KEYS,
"RT PLAN" => RT_PLAN_KEYS,
"RT TREAT RECORD" => RT_TREAT_RECORD_KEYS,
"PRESENTATION" => PRESENTATION_KEYS,
"WAVEFORM" => WAVEFORM_KEYS,
"SR DOCUMENT" => SR_DOCUMENT_KEYS,
"KEY OBJECT DOC" => KEY_OBJECT_DOC_KEYS,
"SPECTROSCOPY" => SPECTROSCOPY_KEYS,
"RAW DATA" => RAW_DATA_KEYS,
"REGISTRATION" | "FIDUCIAL" | "VALUE MAP" | "MEASUREMENT" | "SURFACE" | "SURFACE SCAN"
| "TRACT" | "ANNOTATION" => CONTENT_DATE_TIME_KEYS,
"HANGING PROTOCOL" => HANGING_PROTOCOL_KEYS,
"ENCAP DOC" => ENCAP_DOC_KEYS,
"PALETTE" => PALETTE_KEYS,
"IMPLANT" => IMPLANT_KEYS,
"IMPLANT ASSY" => IMPLANT_ASSY_KEYS,
"IMPLANT GROUP" => IMPLANT_GROUP_KEYS,
"ASSESSMENT" => ASSESSMENT_KEYS,
"RADIOTHERAPY" => RADIOTHERAPY_KEYS,
"INVENTORY" => INVENTORY_KEYS,
"WF PRESENTATION" => WF_PRESENTATION_KEYS,
_ => NO_KEYS,
}
}
struct Record {
record_type: &'static str,
keys: InMemDicomObject,
children: Vec<usize>,
}
struct Tree {
records: Vec<Record>,
roots: Vec<usize>,
}
impl Tree {
fn push(&mut self, record: Record) -> usize {
self.records.push(record);
self.records.len() - 1
}
}
struct Resolved<'a> {
entry: &'a DicomdirEntry,
file_id: String,
sop_class_uid: String,
sop_instance_uid: String,
record_type: &'static str,
}
struct SeriesGroup {
uid: String,
instances: Vec<usize>,
}
struct StudyGroup {
uid: String,
series: Vec<SeriesGroup>,
}
struct PatientGroup {
patient_id: Option<String>,
name: String,
birth_date: String,
studies: Vec<StudyGroup>,
}
fn build_tree(
entries: &[DicomdirEntry],
report: &mut DicomdirReport,
) -> Result<Tree, DicomdirError> {
let mut resolved = Vec::with_capacity(entries.len());
let mut file_ids = HashSet::new();
let mut sop_instances: HashMap<String, String> = HashMap::new();
for entry in entries {
validate_file_id(&entry.file_id)?;
let file_id = entry.file_id.join("\\");
if !file_ids.insert(file_id.clone()) {
return Err(DicomdirError::DuplicateFileId(file_id));
}
let missing = |attribute| DicomdirError::MissingAttribute {
file_id: file_id.clone(),
attribute,
};
let sop_class_uid = entry
.sop_class_uid
.as_deref()
.map(trim_value)
.filter(|s| !s.is_empty())
.map(str::to_owned)
.or_else(|| get_str(&entry.dataset, SOP_CLASS_UID))
.ok_or_else(|| missing("SOPClassUID"))?;
let sop_instance_uid = entry
.sop_instance_uid
.as_deref()
.map(trim_value)
.filter(|s| !s.is_empty())
.map(str::to_owned)
.or_else(|| get_str(&entry.dataset, SOP_INSTANCE_UID))
.ok_or_else(|| missing("SOPInstanceUID"))?;
if trim_value(&entry.transfer_syntax_uid).is_empty() {
return Err(missing("TransferSyntaxUID"));
}
if let Some(previous) = sop_instances.insert(sop_instance_uid.clone(), file_id.clone()) {
return Err(DicomdirError::DuplicateSopInstance {
sop_instance_uid,
file_ids: (previous, file_id),
});
}
let record_type = record_type_for_entry(entry, &sop_class_uid, &file_id)?;
resolved.push(Resolved {
entry,
file_id,
sop_class_uid,
sop_instance_uid,
record_type,
});
}
let mut patients: Vec<PatientGroup> = Vec::new();
let mut root_level: Vec<usize> = Vec::new();
let mut study_owner: HashMap<String, usize> = HashMap::new();
let mut series_owner: HashMap<String, String> = HashMap::new();
for (index, item) in resolved.iter().enumerate() {
if ROOT_LEVEL_RECORD_TYPES.contains(&item.record_type) {
root_level.push(index);
continue;
}
let ds = &item.entry.dataset;
let study_uid = get_str(ds, STUDY_INSTANCE_UID).ok_or(DicomdirError::MissingAttribute {
file_id: item.file_id.clone(),
attribute: "StudyInstanceUID",
})?;
let series_uid =
get_str(ds, SERIES_INSTANCE_UID).ok_or(DicomdirError::MissingAttribute {
file_id: item.file_id.clone(),
attribute: "SeriesInstanceUID",
})?;
let patient_id = get_str(ds, PATIENT_ID);
let name = normalize_name(&get_str(ds, PATIENT_NAME).unwrap_or_default());
let birth_date = get_str(ds, PATIENT_BIRTH_DATE).unwrap_or_default();
let patient_index = match study_owner.get(&study_uid) {
Some(&p) => p,
None => {
let found = patients
.iter()
.position(|p| match (&p.patient_id, &patient_id) {
(Some(a), Some(b)) => a == b,
(None, None) => {
!name.is_empty() && p.name == name && p.birth_date == birth_date
}
_ => false,
});
let p = match found {
Some(p) => p,
None => {
patients.push(PatientGroup {
patient_id: patient_id.clone(),
name: name.clone(),
birth_date: birth_date.clone(),
studies: Vec::new(),
});
patients.len() - 1
}
};
study_owner.insert(study_uid.clone(), p);
p
}
};
let patient = &mut patients[patient_index];
let same_patient = match (&patient.patient_id, &patient_id) {
(Some(a), Some(b)) => a == b,
(None, None) => patient.name == name && patient.birth_date == birth_date,
_ => false,
};
if !same_patient {
return Err(DicomdirError::StudyConflict {
study_instance_uid: study_uid,
});
}
if patient.patient_id.is_some() && !names_match(&patient.name, &name) {
return Err(DicomdirError::PatientConflict {
patient_id: patient.patient_id.clone().unwrap_or_default(),
names: (patient.name.clone(), name),
});
}
if patient.name.is_empty() && !name.is_empty() {
patient.name = name;
}
match series_owner.get(&series_uid) {
Some(owner) if *owner != study_uid => {
return Err(DicomdirError::SeriesConflict {
series_instance_uid: series_uid,
})
}
Some(_) => {}
None => {
series_owner.insert(series_uid.clone(), study_uid.clone());
}
}
let study = match patient.studies.iter_mut().position(|s| s.uid == study_uid) {
Some(s) => &mut patient.studies[s],
None => {
patient.studies.push(StudyGroup {
uid: study_uid,
series: Vec::new(),
});
patient.studies.last_mut().expect("just pushed")
}
};
let series = match study.series.iter_mut().position(|s| s.uid == series_uid) {
Some(s) => &mut study.series[s],
None => {
study.series.push(SeriesGroup {
uid: series_uid,
instances: Vec::new(),
});
study.series.last_mut().expect("just pushed")
}
};
series.instances.push(index);
}
let mut tree = Tree {
records: Vec::new(),
roots: Vec::new(),
};
let used_patient_ids: HashSet<String> = patients
.iter()
.filter_map(|p| p.patient_id.clone())
.collect();
let mut synthetic_patients = 0usize;
let mut synthetic_studies = 0usize;
let used_study_ids: HashSet<String> = resolved
.iter()
.filter_map(|r| get_str(&r.entry.dataset, STUDY_ID))
.collect();
for patient in &patients {
let instances: Vec<&Resolved> = patient
.studies
.iter()
.flat_map(|s| s.series.iter())
.flat_map(|s| s.instances.iter())
.map(|&i| &resolved[i])
.collect();
let mut keys = entity_keys(PATIENT_KEYS, &instances, "PATIENT", report, &patient.name);
if patient.patient_id.is_none() {
let id = loop {
synthetic_patients += 1;
let candidate = format!("SYNTH_PAT_{synthetic_patients:04}");
if !used_patient_ids.contains(&candidate) {
break candidate;
}
};
let entity = if patient.name.is_empty() {
format!("patient of study {}", patient.studies[0].uid)
} else {
patient.name.clone()
};
synthesize(
&mut keys,
report,
"PATIENT",
&entity,
&PATIENT_ID_KEY,
id,
None,
);
}
let patient_record = tree.push(Record {
record_type: "PATIENT",
keys,
children: Vec::new(),
});
tree.roots.push(patient_record);
for study in &patient.studies {
let instances: Vec<&Resolved> = study
.series
.iter()
.flat_map(|s| s.instances.iter())
.map(|&i| &resolved[i])
.collect();
let mut keys = entity_keys(STUDY_KEYS, &instances, "STUDY", report, &study.uid);
if get_str(&keys, STUDY_ID).is_none() {
let id = loop {
synthetic_studies += 1;
let candidate = if synthetic_studies <= 9999 {
format!("SYNTH_STUDY_{synthetic_studies:04}")
} else {
format!("SYNTH_{synthetic_studies}")
};
if !used_study_ids.contains(&candidate) {
break candidate;
}
};
synthesize(
&mut keys,
report,
"STUDY",
&study.uid,
&STUDY_ID_KEY,
id,
None,
);
}
let study_record = tree.push(Record {
record_type: "STUDY",
keys,
children: Vec::new(),
});
tree.records[patient_record].children.push(study_record);
let mut next_series_number = max_number(
study
.series
.iter()
.flat_map(|s| s.instances.iter())
.map(|&i| &resolved[i]),
SERIES_NUMBER,
) + 1;
for series in &study.series {
let instances: Vec<&Resolved> =
series.instances.iter().map(|&i| &resolved[i]).collect();
let mut keys = entity_keys(SERIES_KEYS, &instances, "SERIES", report, &series.uid);
if get_str(&keys, MODALITY).is_none() {
synthesize(
&mut keys,
report,
"SERIES",
&series.uid,
&MODALITY_KEY,
"OT".into(),
None,
);
}
if get_str(&keys, SERIES_NUMBER).is_none() {
let number = next_series_number.to_string();
next_series_number += 1;
synthesize(
&mut keys,
report,
"SERIES",
&series.uid,
&SERIES_NUMBER_KEY,
number,
None,
);
}
let series_record = tree.push(Record {
record_type: "SERIES",
keys,
children: Vec::new(),
});
tree.records[study_record].children.push(series_record);
let mut next_instance_number =
max_number(instances.iter().copied(), INSTANCE_NUMBER) + 1;
for item in &instances {
let mut keys = leaf_record(item, report);
let needs_number = leaf_keys(item.record_type)
.iter()
.any(|k| k.tag == INSTANCE_NUMBER && k.kind == One);
if needs_number && get_str(&keys, INSTANCE_NUMBER).is_none() {
let number = next_instance_number.to_string();
next_instance_number += 1;
synthesize(
&mut keys,
report,
item.record_type,
&item.sop_instance_uid,
&INSTANCE_NUMBER_KEY,
number,
None,
);
}
add_character_set(&mut keys);
let leaf = tree.push(Record {
record_type: item.record_type,
keys,
children: Vec::new(),
});
tree.records[series_record].children.push(leaf);
}
}
}
}
for &index in &root_level {
let item = &resolved[index];
let mut keys = leaf_record(item, report);
add_character_set(&mut keys);
let record = tree.push(Record {
record_type: item.record_type,
keys,
children: Vec::new(),
});
tree.roots.push(record);
}
for record in &mut tree.records {
if matches!(record.record_type, "PATIENT" | "STUDY" | "SERIES") {
add_character_set(&mut record.keys);
}
put_record_header(&mut record.keys, record.record_type);
}
Ok(tree)
}
fn entity_keys(
keys: &[Key],
instances: &[&Resolved],
record_type: &str,
report: &mut DicomdirReport,
entity: &str,
) -> InMemDicomObject {
let mut out = InMemDicomObject::new_empty();
for key in keys {
if let Some(element) = instances
.iter()
.find_map(|r| non_empty_element(&r.entry.dataset, key.tag))
.and_then(|e| key_element(e, key))
{
out.put(element);
continue;
}
match key.kind {
Two => out.put(empty_element(key)),
IfPresent => {}
One => {
let fallback = key.fallbacks.iter().find_map(|&tag| {
instances
.iter()
.find_map(|r| non_empty_element(&r.entry.dataset, tag))
.map(|e| (e, tag))
});
if let Some((element, from)) = fallback {
let value = element_string(element);
synthesize(
&mut out,
report,
record_type,
entity,
key,
value,
Some(from),
);
} else if let Some(value) = last_resort_value(key) {
synthesize(&mut out, report, record_type, entity, key, value, None);
}
}
}
}
out
}
fn last_resort_value(key: &Key) -> Option<String> {
match (key.tag, key.vr) {
(_, VR::DA) if key.tag == STUDY_DATE || !key.fallbacks.is_empty() => Some(
dcmnorm_core::chrono::Local::now()
.format("%Y%m%d")
.to_string(),
),
(_, VR::TM) if key.tag == STUDY_TIME || !key.fallbacks.is_empty() => {
Some("000000".to_owned())
}
_ => None,
}
}
fn leaf_record(item: &Resolved, report: &mut DicomdirReport) -> InMemDicomObject {
let ds = &item.entry.dataset;
let mut out = InMemDicomObject::new_empty();
for key in leaf_keys(item.record_type) {
if let Some(element) =
lookup_leaf_key(ds, key.tag, item.record_type).and_then(|e| key_element(&e, key))
{
out.put(element);
continue;
}
match key.kind {
Two => out.put(empty_element(key)),
IfPresent => {}
One => {
if key.tag == INSTANCE_NUMBER {
continue; }
let fallback = key
.fallbacks
.iter()
.find_map(|&tag| non_empty_element(ds, tag).map(|e| (e, tag)));
if let Some((element, from)) = fallback {
let value = element_string(element);
synthesize(
&mut out,
report,
item.record_type,
&item.sop_instance_uid,
key,
value,
Some(from),
);
} else if let Some(value) = last_resort_value(key) {
synthesize(
&mut out,
report,
item.record_type,
&item.sop_instance_uid,
key,
value,
None,
);
} else {
out.put(empty_element(key));
report.warnings.push(format!(
"{} {}: Type 1 key {} is missing in the instance; written empty",
item.record_type, item.file_id, key.keyword
));
}
}
}
}
match item.record_type {
"SR DOCUMENT" => {
let verified = get_str(ds, VERIFICATION_FLAG).as_deref() == Some("VERIFIED");
if verified {
if let Some(latest) = latest_verification_datetime(ds) {
out.put(DataElement::new(
VERIFICATION_DATE_TIME,
VR::DT,
PrimitiveValue::from(latest),
));
} else {
report.warnings.push(format!(
"SR DOCUMENT {}: VERIFIED but no Verifying Observer Sequence > Verification DateTime",
item.file_id
));
}
}
if let Some(content) = concept_modifier_items(ds) {
out.put(content);
}
}
"KEY OBJECT DOC" => {
if let Some(content) = concept_modifier_items(ds) {
out.put(content);
}
}
_ => {}
}
out.put(DataElement::new(
REFERENCED_FILE_ID,
VR::CS,
PrimitiveValue::Strs(item.entry.file_id.iter().cloned().collect()),
));
out.put(DataElement::new(
REFERENCED_SOP_CLASS_UID_IN_FILE,
VR::UI,
PrimitiveValue::from(item.sop_class_uid.clone()),
));
out.put(DataElement::new(
REFERENCED_SOP_INSTANCE_UID_IN_FILE,
VR::UI,
PrimitiveValue::from(item.sop_instance_uid.clone()),
));
out.put(DataElement::new(
REFERENCED_TRANSFER_SYNTAX_UID_IN_FILE,
VR::UI,
PrimitiveValue::from(trim_value(&item.entry.transfer_syntax_uid).to_owned()),
));
if let Some(related) = ds
.get(RELATED_GENERAL_SOP_CLASS_UID)
.filter(|e| !element_string(e).is_empty())
{
out.put(DataElement::new(
REFERENCED_RELATED_GENERAL_SOP_CLASS_UID_IN_FILE,
VR::UI,
related
.value()
.primitive()
.cloned()
.unwrap_or(PrimitiveValue::Empty),
));
}
out
}
fn lookup_leaf_key(ds: &InMemDicomObject, tag: Tag, record_type: &str) -> Option<InMemElement> {
if let Some(element) = non_empty_element(ds, tag) {
return Some(element.clone());
}
let shared_lookup = matches!(record_type, "IMAGE" | "SPECTROSCOPY")
&& matches!(
tag,
REFERENCED_IMAGE_SEQUENCE
| IMAGE_POSITION_PATIENT
| IMAGE_ORIENTATION_PATIENT
| PIXEL_SPACING
);
if !shared_lookup {
return None;
}
let shared = ds
.get(SHARED_FUNCTIONAL_GROUPS_SEQUENCE)?
.items()?
.first()?;
if let Some(element) = non_empty_element(shared, tag) {
return Some(element.clone());
}
shared.iter().find_map(|group| {
group
.items()?
.first()
.and_then(|item| non_empty_element(item, tag))
.cloned()
})
}
fn concept_modifier_items(ds: &InMemDicomObject) -> Option<InMemElement> {
let items: Vec<InMemDicomObject> = ds
.get(CONTENT_SEQUENCE)?
.items()?
.iter()
.filter(|item| get_str(item, RELATIONSHIP_TYPE).as_deref() == Some("HAS CONCEPT MOD"))
.map(|item| InMemDicomObject::from_element_iter(item.iter().map(clean_element)))
.collect();
(!items.is_empty()).then(|| {
DataElement::new(
CONTENT_SEQUENCE,
VR::SQ,
DataSetSequence::new(items, Length::UNDEFINED),
)
})
}
fn latest_verification_datetime(ds: &InMemDicomObject) -> Option<String> {
ds.get(VERIFYING_OBSERVER_SEQUENCE)?
.items()?
.iter()
.filter_map(|item| get_str(item, VERIFICATION_DATE_TIME))
.max_by(|a, b| normalize_datetime(a).cmp(&normalize_datetime(b)))
}
fn normalize_datetime(dt: &str) -> String {
let core: String = dt.chars().take_while(|c| *c != '+' && *c != '-').collect();
format!("{core:0<21}")
}
fn synthesize(
keys: &mut InMemDicomObject,
report: &mut DicomdirReport,
record_type: &str,
entity: &str,
key: &Key,
value: String,
from: Option<Tag>,
) {
keys.put(DataElement::new(
key.tag,
key.vr,
PrimitiveValue::from(value.clone()),
));
report.synthesized.push(DicomdirSynthesized {
record_type: record_type.into(),
entity: entity.to_owned(),
attribute: key.keyword.into(),
value,
source: from.map(tag_keyword),
});
}
fn tag_keyword(tag: Tag) -> String {
use dcmnorm_core::dictionary::{DataDictionary, DataDictionaryEntry};
dcmnorm_dictionary::StandardDataDictionary
.by_tag(tag)
.map(|entry| entry.alias().to_owned())
.unwrap_or_else(|| format!("({:04X},{:04X})", tag.group(), tag.element()))
}
fn max_number<'r, 'e: 'r>(instances: impl IntoIterator<Item = &'r Resolved<'e>>, tag: Tag) -> i64 {
instances
.into_iter()
.filter_map(|r| get_str(&r.entry.dataset, tag))
.filter_map(|s| s.trim().parse::<f64>().ok())
.map(|n| n as i64)
.max()
.unwrap_or(0)
.max(0)
}
fn put_record_header(keys: &mut InMemDicomObject, record_type: &str) {
keys.put(DataElement::new(
OFFSET_NEXT,
VR::UL,
PrimitiveValue::from(0u32),
));
keys.put(DataElement::new(
RECORD_IN_USE_FLAG,
VR::US,
PrimitiveValue::from(0xFFFFu16),
));
keys.put(DataElement::new(
OFFSET_LOWER,
VR::UL,
PrimitiveValue::from(0u32),
));
keys.put(DataElement::new(
DIRECTORY_RECORD_TYPE,
VR::CS,
PrimitiveValue::from(record_type.to_owned()),
));
}
fn add_character_set(keys: &mut InMemDicomObject) {
if has_non_ascii_text(keys) {
keys.put(DataElement::new(
SPECIFIC_CHARACTER_SET,
VR::CS,
PrimitiveValue::from(UTF8_CHARACTER_SET.to_owned()),
));
} else {
keys.remove_element(SPECIFIC_CHARACTER_SET);
}
}
fn has_non_ascii_text(object: &InMemDicomObject) -> bool {
object.iter().any(|element| match element.value() {
Value::Primitive(PrimitiveValue::Str(s)) => !s.is_ascii(),
Value::Primitive(PrimitiveValue::Strs(values)) => values.iter().any(|s| !s.is_ascii()),
Value::Sequence(seq) => seq.items().iter().any(has_non_ascii_text),
_ => false,
})
}
fn encode_dicomdir(
tree: &Tree,
file_set_id: &str,
sop_instance_uid: Option<&str>,
) -> Result<Vec<u8>, DicomdirError> {
let ts = TransferSyntaxRegistry
.get(uids::EXPLICIT_VR_LITTLE_ENDIAN)
.expect("Explicit VR Little Endian must be registered");
let instance_uid = sop_instance_uid
.map(str::to_owned)
.unwrap_or_else(generate_uid);
let meta = FileMetaTableBuilder::new()
.information_version([0x00, 0x01])
.media_storage_sop_class_uid(MEDIA_STORAGE_DIRECTORY_STORAGE)
.media_storage_sop_instance_uid(instance_uid)
.transfer_syntax(uids::EXPLICIT_VR_LITTLE_ENDIAN)
.build()
.map_err(|e| DicomdirError::Io(std::io::Error::other(e.to_string())))?;
let mut meta_bytes = Vec::new();
meta.write_to(&mut meta_bytes)
.map_err(DicomdirError::Write)?;
let mut order = Vec::with_capacity(tree.records.len());
let mut stack: Vec<usize> = tree.roots.iter().rev().copied().collect();
while let Some(index) = stack.pop() {
order.push(index);
stack.extend(tree.records[index].children.iter().rev().copied());
}
let header_with = |first: u32, last: u32| -> Result<Vec<u8>, DicomdirError> {
let header = InMemDicomObject::from_element_iter([
DataElement::new(
FILE_SET_ID,
VR::CS,
PrimitiveValue::from(file_set_id.to_owned()),
),
DataElement::new(OFFSET_FIRST_ROOT, VR::UL, PrimitiveValue::from(first)),
DataElement::new(OFFSET_LAST_ROOT, VR::UL, PrimitiveValue::from(last)),
DataElement::new(
FILE_SET_CONSISTENCY_FLAG,
VR::US,
PrimitiveValue::from(0x0000u16),
),
]);
let mut bytes = Vec::new();
header
.write_dataset_with_ts(&mut bytes, ts)
.map_err(DicomdirError::Write)?;
Ok(bytes)
};
let encode = |keys: &InMemDicomObject| -> Result<Vec<u8>, DicomdirError> {
let mut bytes = Vec::new();
keys.write_dataset_with_ts(&mut bytes, ts)
.map_err(DicomdirError::Write)?;
Ok(bytes)
};
let header_len = header_with(0, 0)?.len();
const SEQUENCE_HEADER_LEN: usize = 12; let mut offsets = vec![0u32; tree.records.len()];
let mut position = meta_bytes.len() + header_len + SEQUENCE_HEADER_LEN;
let mut measured = vec![0usize; tree.records.len()];
for &index in &order {
let len = encode(&tree.records[index].keys)?.len();
offsets[index] = u32::try_from(position).map_err(|_| too_large())?;
measured[index] = len;
position += 8 + len;
}
let mut next_sibling = vec![None; tree.records.len()];
for siblings in std::iter::once(&tree.roots).chain(tree.records.iter().map(|r| &r.children)) {
for pair in siblings.windows(2) {
next_sibling[pair[0]] = Some(pair[1]);
}
}
let offset_of_first = |indices: &[usize]| indices.first().map(|&i| offsets[i]).unwrap_or(0);
let mut items = Vec::with_capacity(position);
for &index in &order {
let record = &tree.records[index];
let mut keys = record.keys.clone();
keys.put(DataElement::new(
OFFSET_LOWER,
VR::UL,
PrimitiveValue::from(offset_of_first(&record.children)),
));
let next = next_sibling[index].map(|i| offsets[i]).unwrap_or(0);
keys.put(DataElement::new(
OFFSET_NEXT,
VR::UL,
PrimitiveValue::from(next),
));
let bytes = encode(&keys)?;
assert_eq!(
bytes.len(),
measured[index],
"a DICOMDIR record changed size when its offsets were filled in"
);
items.extend_from_slice(&ITEM_TAG_BYTES);
items.extend_from_slice(
&u32::try_from(bytes.len())
.map_err(|_| too_large())?
.to_le_bytes(),
);
items.extend_from_slice(&bytes);
}
let first = offset_of_first(&tree.roots);
let last = tree.roots.last().map(|&i| offsets[i]).unwrap_or(0);
let header = header_with(first, last)?;
debug_assert_eq!(header.len(), header_len);
let mut out = meta_bytes;
out.extend_from_slice(&header);
out.extend_from_slice(&DIRECTORY_RECORD_SEQUENCE.group().to_le_bytes());
out.extend_from_slice(&DIRECTORY_RECORD_SEQUENCE.element().to_le_bytes());
out.extend_from_slice(b"SQ");
out.extend_from_slice(&[0, 0]);
out.extend_from_slice(
&u32::try_from(items.len())
.map_err(|_| too_large())?
.to_le_bytes(),
);
out.extend_from_slice(&items);
Ok(out)
}
fn too_large() -> DicomdirError {
DicomdirError::Io(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"DICOMDIR would exceed 4 GiB",
))
}
fn trim_value(s: &str) -> &str {
s.trim_matches(|c: char| c == ' ' || c == '\0')
}
fn element_string(element: &InMemElement) -> String {
if element.vr() == VR::UN {
return element
.to_bytes()
.map(|b| trim_value(&String::from_utf8_lossy(&b)).to_owned())
.unwrap_or_default();
}
element
.to_str()
.map(|s| trim_value(&s).to_owned())
.unwrap_or_default()
}
fn key_element(element: &InMemElement, key: &Key) -> Option<InMemElement> {
if element.vr() != VR::UN || key.vr == VR::UN {
return Some(clean_element(element));
}
let bytes = element.to_bytes().ok()?;
let value = match key.vr {
VR::SQ => return None,
VR::US => PrimitiveValue::U16(
bytes
.chunks_exact(2)
.map(|c| u16::from_le_bytes([c[0], c[1]]))
.collect(),
),
VR::UL => PrimitiveValue::U32(
bytes
.chunks_exact(4)
.map(|c| u32::from_le_bytes([c[0], c[1], c[2], c[3]]))
.collect(),
),
_ => {
let text = String::from_utf8_lossy(&bytes);
let values: Vec<String> = text.split('\\').map(|v| trim_value(v).to_owned()).collect();
if values.len() == 1 {
PrimitiveValue::from(values.into_iter().next().unwrap_or_default())
} else {
PrimitiveValue::Strs(values.into_iter().collect())
}
}
};
Some(DataElement::new(key.tag, key.vr, value))
}
fn get_str(ds: &InMemDicomObject, tag: Tag) -> Option<String> {
ds.get(tag).map(element_string).filter(|s| !s.is_empty())
}
fn non_empty_element(ds: &InMemDicomObject, tag: Tag) -> Option<&InMemElement> {
let element = ds.get(tag)?;
let has_value = match element.value() {
Value::Sequence(seq) => !seq.items().is_empty(),
Value::Primitive(PrimitiveValue::Empty) => false,
Value::Primitive(PrimitiveValue::Str(_) | PrimitiveValue::Strs(_)) => {
!element_string(element).is_empty()
}
Value::Primitive(_) if element.vr() == VR::UN => !element_string(element).is_empty(),
Value::Primitive(_) => true,
Value::PixelSequence(_) => false,
};
has_value.then_some(element)
}
fn empty_element(key: &Key) -> InMemElement {
if key.vr == VR::SQ {
DataElement::new(
key.tag,
VR::SQ,
DataSetSequence::<InMemDicomObject>::new(Vec::new(), Length::UNDEFINED),
)
} else {
DataElement::new(key.tag, key.vr, PrimitiveValue::Empty)
}
}
fn clean_element(element: &InMemElement) -> InMemElement {
let vr = element.vr();
match element.value() {
Value::Sequence(seq) => {
let items: Vec<InMemDicomObject> = seq
.items()
.iter()
.map(|item| InMemDicomObject::from_element_iter(item.iter().map(clean_element)))
.collect();
DataElement::new(
element.tag(),
VR::SQ,
DataSetSequence::new(items, Length::UNDEFINED),
)
}
Value::Primitive(PrimitiveValue::Str(text)) => DataElement::new(
element.tag(),
vr,
PrimitiveValue::from(clean_text(text, vr)),
),
Value::Primitive(PrimitiveValue::Strs(values)) => DataElement::new(
element.tag(),
vr,
PrimitiveValue::Strs(values.iter().map(|v| clean_text(v, vr)).collect()),
),
_ => element.clone(),
}
}
fn clean_text(text: &str, vr: VR) -> String {
let trimmed = text.trim_end_matches(['\0', ' ']);
let leading_insignificant = matches!(
vr,
VR::AE | VR::CS | VR::DA | VR::DS | VR::DT | VR::IS | VR::LO | VR::SH | VR::TM | VR::UI
);
if leading_insignificant {
trimmed.trim_start_matches(' ')
} else {
trimmed
}
.to_owned()
}
fn normalize_name(name: &str) -> String {
name.split('=')
.map(|group| group.trim_end_matches(['^', ' ']).trim())
.collect::<Vec<_>>()
.join("=")
.trim_end_matches('=')
.to_owned()
}
fn names_match(a: &str, b: &str) -> bool {
a.is_empty() || b.is_empty() || a.eq_ignore_ascii_case(b)
}
fn strip_bulk_data_uris(value: &mut JsonValue) {
match value {
JsonValue::Object(map) => {
map.retain(|_, member| !matches!(member, JsonValue::Object(m) if m.contains_key("BulkDataURI")));
for member in map.values_mut() {
strip_bulk_data_uris(member);
}
}
JsonValue::Array(items) => items.iter_mut().for_each(strip_bulk_data_uris),
_ => {}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::BTreeMap;
#[derive(Debug)]
struct RawRecord {
offset: u32,
elements: BTreeMap<(u16, u16), Vec<u8>>,
}
impl RawRecord {
fn ul(&self, g: u16, e: u16) -> u32 {
let v = &self.elements[&(g, e)];
u32::from_le_bytes([v[0], v[1], v[2], v[3]])
}
fn text(&self, g: u16, e: u16) -> Option<String> {
self.elements.get(&(g, e)).map(|v| {
String::from_utf8_lossy(v)
.trim_end_matches([' ', '\0'])
.to_owned()
})
}
fn record_type(&self) -> String {
self.text(0x0004, 0x1430).unwrap()
}
}
struct RawDicomdir {
top: BTreeMap<(u16, u16), Vec<u8>>,
records: Vec<RawRecord>,
}
fn u16_at(b: &[u8], p: usize) -> u16 {
u16::from_le_bytes([b[p], b[p + 1]])
}
fn u32_at(b: &[u8], p: usize) -> u32 {
u32::from_le_bytes([b[p], b[p + 1], b[p + 2], b[p + 3]])
}
fn header(b: &[u8], p: usize) -> ((u16, u16), [u8; 2], u32, usize) {
let tag = (u16_at(b, p), u16_at(b, p + 2));
let vr = [b[p + 4], b[p + 5]];
match &vr {
b"OB" | b"OW" | b"OF" | b"SQ" | b"UT" | b"UN" | b"UC" | b"UR" | b"OD" | b"OL"
| b"OV" | b"SV" | b"UV" => (tag, vr, u32_at(b, p + 8), 12),
_ => (tag, vr, u16_at(b, p + 6) as u32, 8),
}
}
fn skip_undefined_sequence(b: &[u8], mut p: usize) -> usize {
loop {
let tag = (u16_at(b, p), u16_at(b, p + 2));
let len = u32_at(b, p + 4);
p += 8;
match tag {
(0xFFFE, 0xE0DD) => return p,
(0xFFFE, 0xE000) if len == 0xFFFF_FFFF => p = skip_undefined_item(b, p),
(0xFFFE, 0xE000) => p += len as usize,
other => panic!("unexpected {other:04X?} in sequence"),
}
}
}
fn skip_undefined_item(b: &[u8], mut p: usize) -> usize {
loop {
if (u16_at(b, p), u16_at(b, p + 2)) == (0xFFFE, 0xE00D) {
return p + 8;
}
p = skip_element(b, p);
}
}
fn skip_element(b: &[u8], p: usize) -> usize {
let (_, vr, len, hlen) = header(b, p);
if &vr == b"SQ" && len == 0xFFFF_FFFF {
skip_undefined_sequence(b, p + hlen)
} else {
p + hlen + len as usize
}
}
fn parse_elements(b: &[u8], mut p: usize, end: usize) -> BTreeMap<(u16, u16), Vec<u8>> {
let mut out = BTreeMap::new();
while p < end {
let (tag, _, len, hlen) = header(b, p);
let next = skip_element(b, p);
let value_end = if len == 0xFFFF_FFFF {
next
} else {
p + hlen + len as usize
};
out.insert(tag, b[p + hlen..value_end].to_vec());
p = next;
}
assert_eq!(p, end, "element walk overran its container");
out
}
fn parse_raw(b: &[u8]) -> RawDicomdir {
assert_eq!(&b[128..132], b"DICM");
let (tag, _, _, hlen) = header(b, 132);
assert_eq!(tag, (0x0002, 0x0000));
let group_len = u32_at(b, 132 + hlen);
let dataset_start = 132 + hlen + 4 + group_len as usize;
let mut top = BTreeMap::new();
let mut records = Vec::new();
let mut p = dataset_start;
while p < b.len() {
let (tag, vr, len, hlen) = header(b, p);
if tag == (0x0004, 0x1220) {
assert_eq!(&vr, b"SQ");
assert_ne!(
len, 0xFFFF_FFFF,
"the record sequence is written with a defined length"
);
let end = p + hlen + len as usize;
let mut q = p + hlen;
while q < end {
assert_eq!((u16_at(b, q), u16_at(b, q + 2)), (0xFFFE, 0xE000));
let item_len = u32_at(b, q + 4) as usize;
records.push(RawRecord {
offset: q as u32,
elements: parse_elements(b, q + 8, q + 8 + item_len),
});
q += 8 + item_len;
}
p = end;
} else {
top.insert(tag, b[p + hlen..p + hlen + len as usize].to_vec());
p += hlen + len as usize;
}
}
RawDicomdir { top, records }
}
fn walk(raw: &RawDicomdir) -> Vec<(usize, usize)> {
let by_offset: HashMap<u32, usize> = raw
.records
.iter()
.enumerate()
.map(|(i, r)| (r.offset, i))
.collect();
let first = u32_at(&raw.top[&(0x0004, 0x1200)], 0);
let last = u32_at(&raw.top[&(0x0004, 0x1202)], 0);
let mut out = Vec::new();
fn entity(
raw: &RawDicomdir,
by_offset: &HashMap<u32, usize>,
mut offset: u32,
depth: usize,
out: &mut Vec<(usize, usize)>,
) -> u32 {
let mut last = 0;
while offset != 0 {
let index = *by_offset
.get(&offset)
.unwrap_or_else(|| panic!("offset {offset} is not a record"));
out.push((depth, index));
last = offset;
let record = &raw.records[index];
assert_eq!(
record.elements[&(0x0004, 0x1410)],
vec![0xFF, 0xFF],
"Record In-use Flag FFFFH"
);
let lower = record.ul(0x0004, 0x1420);
if lower != 0 {
entity(raw, by_offset, lower, depth + 1, out);
}
offset = record.ul(0x0004, 0x1400);
}
last
}
let found_last = entity(raw, &by_offset, first, 0, &mut out);
assert_eq!(found_last, last, "(0004,1202) must be the last root record");
out
}
fn ds(elements: &[(u16, u16, VR, &str)]) -> InMemDicomObject {
InMemDicomObject::from_element_iter(elements.iter().map(|&(g, e, vr, v)| {
DataElement::new(Tag(g, e), vr, PrimitiveValue::from(v.to_owned()))
}))
}
fn image(
patient: &str,
study: &str,
series: &str,
sop: &str,
number: &str,
) -> InMemDicomObject {
let mut out = ds(&[
(0x0008, 0x0016, VR::UI, "1.2.840.10008.5.1.4.1.1.2"),
(0x0008, 0x0018, VR::UI, sop),
(0x0008, 0x0020, VR::DA, "20240102"),
(0x0008, 0x0030, VR::TM, "101500"),
(0x0008, 0x0050, VR::SH, "ACC1"),
(0x0008, 0x0060, VR::CS, "CT"),
(0x0010, 0x0010, VR::PN, "DOE^JANE"),
(0x0010, 0x0020, VR::LO, patient),
(0x0020, 0x000D, VR::UI, study),
(0x0020, 0x000E, VR::UI, series),
(0x0020, 0x0010, VR::SH, "S1"),
(0x0020, 0x0011, VR::IS, "3"),
(0x0020, 0x0013, VR::IS, number),
]);
out.put(DataElement::new(ROWS, VR::US, PrimitiveValue::from(16u16)));
out.put(DataElement::new(
COLUMNS,
VR::US,
PrimitiveValue::from(16u16),
));
out
}
fn entry(file_id: &str, dataset: InMemDicomObject) -> DicomdirEntry {
DicomdirEntry {
file_id: file_id.split('/').map(str::to_owned).collect(),
dataset,
transfer_syntax_uid: uids::EXPLICIT_VR_LITTLE_ENDIAN.to_owned(),
sop_class_uid: None,
sop_instance_uid: None,
}
}
fn options() -> DicomdirOptions {
DicomdirOptions {
file_set_id: "TESTSET".into(),
sop_instance_uid: Some("2.25.1".into()),
}
}
fn fixture(name: &str) -> std::path::PathBuf {
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("test")
.join("files")
.join(name)
}
#[test]
fn offsets_link_every_record_depth_first() {
let entries = vec![
entry(
"DICOM/P1/ST1/SE1/IM1",
image("P1", "1.2.1", "1.2.1.1", "1.2.1.1.1", "1"),
),
entry(
"DICOM/P1/ST1/SE1/IM2",
image("P1", "1.2.1", "1.2.1.1", "1.2.1.1.2", "2"),
),
entry(
"DICOM/P1/ST1/SE2/IM1",
image("P1", "1.2.1", "1.2.1.2", "1.2.1.2.1", "1"),
),
entry(
"DICOM/P1/ST2/SE1/IM1",
image("P1", "1.2.2", "1.2.2.1", "1.2.2.1.1", "1"),
),
entry(
"DICOM/P2/ST1/SE1/IM1",
image("P2", "1.3.1", "1.3.1.1", "1.3.1.1.1", "1"),
),
];
let (bytes, report) = build_dicomdir(&entries, &options()).unwrap();
let raw = parse_raw(&bytes);
assert_eq!(raw.records.len(), 2 + 3 + 4 + 5);
assert_eq!(report.record_count, 14);
let walked = walk(&raw);
assert_eq!(
walked.len(),
raw.records.len(),
"every record reachable exactly once"
);
let shape: Vec<(usize, String)> = walked
.iter()
.map(|&(d, i)| (d, raw.records[i].record_type()))
.collect();
let expected = [
(0, "PATIENT"),
(1, "STUDY"),
(2, "SERIES"),
(3, "IMAGE"),
(3, "IMAGE"),
(2, "SERIES"),
(3, "IMAGE"),
(1, "STUDY"),
(2, "SERIES"),
(3, "IMAGE"),
(0, "PATIENT"),
(1, "STUDY"),
(2, "SERIES"),
(3, "IMAGE"),
];
assert_eq!(
shape,
expected
.iter()
.map(|&(d, t)| (d, t.to_owned()))
.collect::<Vec<_>>()
);
let file_order: Vec<usize> = walked.iter().map(|&(_, i)| i).collect();
assert_eq!(file_order, (0..raw.records.len()).collect::<Vec<_>>());
assert_eq!(
String::from_utf8_lossy(&raw.top[&(0x0004, 0x1130)]).trim_end(),
"TESTSET"
);
assert_eq!(raw.top[&(0x0004, 0x1212)], vec![0, 0]);
let image = &raw.records[3];
assert_eq!(
image.text(0x0004, 0x1500).unwrap(),
"DICOM\\P1\\ST1\\SE1\\IM1"
);
assert_eq!(
image.text(0x0004, 0x1510).unwrap(),
"1.2.840.10008.5.1.4.1.1.2"
);
assert_eq!(image.text(0x0004, 0x1511).unwrap(), "1.2.1.1.1");
assert_eq!(
image.text(0x0004, 0x1512).unwrap(),
uids::EXPLICIT_VR_LITTLE_ENDIAN
);
assert_eq!(
image.ul(0x0004, 0x1420),
0,
"an IMAGE has no lower-level entity"
);
assert!(!raw.records[0].elements.contains_key(&(0x0004, 0x1500)));
assert!(report.synthesized.is_empty(), "{:?}", report.synthesized);
}
#[test]
fn written_file_reads_back_as_media_storage_directory() {
let entries = vec![entry(
"IM1",
image("P1", "1.2.1", "1.2.1.1", "1.2.1.1.1", "1"),
)];
let (bytes, _) = build_dicomdir(&entries, &options()).unwrap();
let object = crate::dicom_io::read_dicom_bytes(&bytes).unwrap();
assert_eq!(
object.meta().media_storage_sop_class_uid(),
MEDIA_STORAGE_DIRECTORY_STORAGE
);
assert_eq!(
trim_value(object.meta().transfer_syntax()),
uids::EXPLICIT_VR_LITTLE_ENDIAN
);
assert_eq!(object.meta().media_storage_sop_instance_uid(), "2.25.1");
assert_eq!(object.meta().information_version, [0, 1]);
let records = object
.get(DIRECTORY_RECORD_SEQUENCE)
.unwrap()
.items()
.unwrap();
assert_eq!(records.len(), 4);
}
#[test]
fn required_keys_per_level() {
let entries = vec![entry(
"IM1",
image("P1", "1.2.1", "1.2.1.1", "1.2.1.1.1", "7"),
)];
let (bytes, _) = build_dicomdir(&entries, &options()).unwrap();
let raw = parse_raw(&bytes);
let [patient, study, series, image] = &raw.records[..] else {
panic!()
};
assert_eq!(patient.text(0x0010, 0x0020).unwrap(), "P1");
assert_eq!(patient.text(0x0010, 0x0010).unwrap(), "DOE^JANE");
assert_eq!(study.text(0x0008, 0x0020).unwrap(), "20240102");
assert_eq!(study.text(0x0008, 0x0030).unwrap(), "101500");
assert_eq!(study.text(0x0020, 0x000D).unwrap(), "1.2.1");
assert_eq!(study.text(0x0020, 0x0010).unwrap(), "S1");
assert_eq!(study.text(0x0008, 0x0050).unwrap(), "ACC1");
assert_eq!(
study.text(0x0008, 0x1030).unwrap(),
"",
"Type 2 Study Description present, empty"
);
assert_eq!(series.text(0x0008, 0x0060).unwrap(), "CT");
assert_eq!(series.text(0x0020, 0x000E).unwrap(), "1.2.1.1");
assert_eq!(series.text(0x0020, 0x0011).unwrap(), "3");
assert_eq!(image.text(0x0020, 0x0013).unwrap(), "7");
assert!(
image.elements.contains_key(&(0x0028, 0x0010)),
"H.3-2 Rows copied"
);
for record in &raw.records {
assert!(
!record.elements.contains_key(&(0x0008, 0x0005)),
"ASCII-only records carry no charset"
);
}
}
#[test]
fn synthesizes_ihe_type1_keys_distinct_per_entity() {
let strip = |mut d: InMemDicomObject, tags: &[Tag]| {
for t in tags {
d.remove_element(*t);
}
d
};
let gone = [
PATIENT_ID,
STUDY_ID,
STUDY_DATE,
STUDY_TIME,
MODALITY,
SERIES_NUMBER,
INSTANCE_NUMBER,
];
let mut a = strip(image("", "1.2.1", "1.2.1.1", "1.2.1.1.1", ""), &gone);
a.put(DataElement::new(
SERIES_DATE,
VR::DA,
PrimitiveValue::from("20230505".to_owned()),
));
let b = strip(image("", "1.2.1", "1.2.1.1", "1.2.1.1.2", ""), &gone);
let c = strip(image("", "1.2.1", "1.2.1.2", "1.2.1.2.1", ""), &gone);
let d = strip(image("", "1.2.2", "1.2.2.1", "1.2.2.1.1", ""), &gone);
let entries = vec![entry("A", a), entry("B", b), entry("C", c), entry("D", d)];
let (bytes, report) = build_dicomdir(&entries, &options()).unwrap();
let raw = parse_raw(&bytes);
let of_type = |t: &str| {
raw.records
.iter()
.filter(|r| r.record_type() == t)
.collect::<Vec<_>>()
};
let patients = of_type("PATIENT");
assert_eq!(
patients.len(),
1,
"same name and birth date, no ID: one patient"
);
assert_eq!(patients[0].text(0x0010, 0x0020).unwrap(), "SYNTH_PAT_0001");
let studies = of_type("STUDY");
let ids: HashSet<_> = studies
.iter()
.map(|s| s.text(0x0020, 0x0010).unwrap())
.collect();
assert_eq!(ids.len(), 2, "two studies get two Study IDs");
assert_eq!(
studies[0].text(0x0008, 0x0020).unwrap(),
"20230505",
"Study Date from Series Date"
);
assert_eq!(studies[0].text(0x0008, 0x0030).unwrap(), "000000");
assert_eq!(
studies[1].text(0x0008, 0x0020).unwrap().len(),
8,
"no date anywhere: today's"
);
let series = of_type("SERIES");
assert!(series
.iter()
.all(|s| s.text(0x0008, 0x0060).unwrap() == "OT"));
assert_eq!(series[0].text(0x0020, 0x0011).unwrap(), "1");
assert_eq!(
series[1].text(0x0020, 0x0011).unwrap(),
"2",
"distinct within the study"
);
let images = of_type("IMAGE");
assert_eq!(images[0].text(0x0020, 0x0013).unwrap(), "1");
assert_eq!(
images[1].text(0x0020, 0x0013).unwrap(),
"2",
"distinct within the series"
);
let synthesized: HashSet<_> = report
.synthesized
.iter()
.map(|s| (s.record_type.as_str(), s.attribute.as_str()))
.collect();
for expected in [
("PATIENT", "PatientID"),
("STUDY", "StudyID"),
("STUDY", "StudyDate"),
("STUDY", "StudyTime"),
("SERIES", "Modality"),
("SERIES", "SeriesNumber"),
("IMAGE", "InstanceNumber"),
] {
assert!(
synthesized.contains(&expected),
"{expected:?} not reported: {:?}",
report.synthesized
);
}
let from_series_date = report
.synthesized
.iter()
.find(|s| s.attribute == "StudyDate" && s.value == "20230505")
.unwrap();
assert_eq!(from_series_date.source.as_deref(), Some("SeriesDate"));
}
#[test]
fn studies_without_patient_id_or_name_are_separate_patients() {
let anonymous = |study: &str| {
let mut d = image(
"",
study,
&format!("{study}.1"),
&format!("{study}.1.1"),
"1",
);
d.remove_element(PATIENT_ID);
d.remove_element(PATIENT_NAME);
d
};
let entries = vec![
entry("A", anonymous("1.5.1")),
entry("B", anonymous("1.5.2")),
];
let (bytes, report) = build_dicomdir(&entries, &options()).unwrap();
let raw = parse_raw(&bytes);
let ids: Vec<_> = raw
.records
.iter()
.filter(|r| r.record_type() == "PATIENT")
.map(|r| r.text(0x0010, 0x0020).unwrap())
.collect();
assert_eq!(ids, ["SYNTH_PAT_0001", "SYNTH_PAT_0002"]);
assert_eq!(
report
.synthesized
.iter()
.filter(|s| s.attribute == "PatientID")
.count(),
2
);
}
#[test]
fn synthesized_numbers_skip_existing_ones() {
let mut no_number = image("P1", "1.2.1", "1.2.1.1", "1.2.1.1.2", "");
no_number.remove_element(INSTANCE_NUMBER);
let entries = vec![
entry("A", image("P1", "1.2.1", "1.2.1.1", "1.2.1.1.1", "5")),
entry("B", no_number),
];
let (bytes, _) = build_dicomdir(&entries, &options()).unwrap();
let raw = parse_raw(&bytes);
let numbers: Vec<_> = raw
.records
.iter()
.filter(|r| r.record_type() == "IMAGE")
.map(|r| r.text(0x0020, 0x0013).unwrap())
.collect();
assert_eq!(numbers, ["5", "6"]);
}
#[test]
fn record_types_for_non_image_objects() {
let leaf = |sop_class: &str, sop: &str, extra: &[(u16, u16, VR, &str)]| {
let mut d = ds(&[
(0x0008, 0x0016, VR::UI, sop_class),
(0x0008, 0x0018, VR::UI, sop),
(0x0008, 0x0023, VR::DA, "20240101"),
(0x0008, 0x0033, VR::TM, "120000"),
(0x0010, 0x0020, VR::LO, "P1"),
(0x0020, 0x000D, VR::UI, "1.2.1"),
(0x0020, 0x000E, VR::UI, &format!("{sop}.9")),
(0x0020, 0x0013, VR::IS, "1"),
]);
for &(g, e, vr, v) in extra {
d.put(DataElement::new(
Tag(g, e),
vr,
PrimitiveValue::from(v.to_owned()),
));
}
d
};
let mut sr = leaf(
"1.2.840.10008.5.1.4.1.1.88.33",
"1.9.1",
&[
(0x0040, 0xA491, VR::CS, "COMPLETE"),
(0x0040, 0xA493, VR::CS, "VERIFIED"),
],
);
let code = ds(&[
(0x0008, 0x0100, VR::SH, "18748-4"),
(0x0008, 0x0102, VR::SH, "LN"),
(0x0008, 0x0104, VR::LO, "Diagnostic Imaging Report"),
]);
sr.put(DataElement::new(
Tag(0x0040, 0xA043),
VR::SQ,
DataSetSequence::new(vec![code], Length::UNDEFINED),
));
let observers = vec![
ds(&[(0x0040, 0xA030, VR::DT, "20240101120000")]),
ds(&[(0x0040, 0xA030, VR::DT, "20240103090000")]),
];
sr.put(DataElement::new(
VERIFYING_OBSERVER_SEQUENCE,
VR::SQ,
DataSetSequence::new(observers, Length::UNDEFINED),
));
let content = vec![
ds(&[
(0x0040, 0xA010, VR::CS, "HAS CONCEPT MOD"),
(0x0040, 0xA040, VR::CS, "CODE"),
]),
ds(&[
(0x0040, 0xA010, VR::CS, "CONTAINS"),
(0x0040, 0xA040, VR::CS, "TEXT"),
]),
];
sr.put(DataElement::new(
CONTENT_SEQUENCE,
VR::SQ,
DataSetSequence::new(content, Length::UNDEFINED),
));
let mut kos = leaf("1.2.840.10008.5.1.4.1.1.88.59", "1.9.2", &[]);
let title = ds(&[
(0x0008, 0x0100, VR::SH, "113000"),
(0x0008, 0x0102, VR::SH, "DCM"),
(0x0008, 0x0104, VR::LO, "Of Interest"),
]);
kos.put(DataElement::new(
Tag(0x0040, 0xA043),
VR::SQ,
DataSetSequence::new(vec![title], Length::UNDEFINED),
));
let cases: Vec<(&str, InMemDicomObject)> = vec![
("SR DOCUMENT", sr),
("KEY OBJECT DOC", kos),
(
"PRESENTATION",
leaf(
"1.2.840.10008.5.1.4.1.1.11.1",
"1.9.3",
&[
(0x0070, 0x0082, VR::DA, "20240101"),
(0x0070, 0x0083, VR::TM, "1200"),
],
),
),
(
"ENCAP DOC",
leaf(
"1.2.840.10008.5.1.4.1.1.104.1",
"1.9.4",
&[(0x0042, 0x0012, VR::LO, "application/pdf")],
),
),
(
"WAVEFORM",
leaf("1.2.840.10008.5.1.4.1.1.9.1.1", "1.9.5", &[]),
),
(
"RT DOSE",
leaf(
"1.2.840.10008.5.1.4.1.1.481.2",
"1.9.6",
&[(0x3004, 0x000A, VR::CS, "PLAN")],
),
),
(
"RT STRUCTURE SET",
leaf(
"1.2.840.10008.5.1.4.1.1.481.3",
"1.9.7",
&[(0x3006, 0x0002, VR::SH, "RS")],
),
),
(
"RT PLAN",
leaf(
"1.2.840.10008.5.1.4.1.1.481.5",
"1.9.8",
&[(0x300A, 0x0002, VR::SH, "PLAN1")],
),
),
(
"REGISTRATION",
leaf("1.2.840.10008.5.1.4.1.1.66.1", "1.9.9", &[]),
),
(
"RADIOTHERAPY",
leaf("1.2.840.10008.5.1.4.1.1.481.10", "1.9.10", &[]),
),
(
"WF PRESENTATION",
leaf(
"1.2.840.10008.5.1.4.1.1.9.100.1",
"1.9.11",
&[
(0x0070, 0x0082, VR::DA, "20240101"),
(0x0070, 0x0083, VR::TM, "1200"),
],
),
),
(
"MEASUREMENT",
leaf("1.2.840.10008.5.1.4.1.1.78.1", "1.9.12", &[]),
),
(
"ANNOTATION",
leaf("1.2.840.10008.5.1.4.1.1.91.1", "1.9.13", &[]),
),
("IMAGE", image("P1", "1.2.1", "1.2.1.77", "1.9.14", "1")),
];
let entries: Vec<DicomdirEntry> = cases
.iter()
.enumerate()
.map(|(i, (_, d))| entry(&format!("F{i}"), d.clone()))
.collect();
let (bytes, report) = build_dicomdir(&entries, &options()).unwrap();
let raw = parse_raw(&bytes);
for (i, (expected, _)) in cases.iter().enumerate() {
let record = raw
.records
.iter()
.find(|r| r.text(0x0004, 0x1500).as_deref() == Some(&format!("F{i}")))
.unwrap();
assert_eq!(record.record_type(), *expected, "F{i}");
}
let sr = raw
.records
.iter()
.find(|r| r.record_type() == "SR DOCUMENT")
.unwrap();
assert_eq!(
sr.text(0x0040, 0xA030).unwrap(),
"20240103090000",
"latest verification"
);
assert_eq!(sr.text(0x0040, 0xA491).unwrap(), "COMPLETE");
let content = &sr.elements[&(0x0040, 0xA730)];
assert!(content.windows(15).any(|w| w == b"HAS CONCEPT MOD"));
assert!(
!content.windows(8).any(|w| w == b"CONTAINS"),
"only HAS CONCEPT MOD items"
);
let doc = raw
.records
.iter()
.find(|r| r.record_type() == "ENCAP DOC")
.unwrap();
assert_eq!(doc.text(0x0042, 0x0012).unwrap(), "application/pdf");
assert!(
doc.elements.contains_key(&(0x0042, 0x0010)),
"Type 2 Document Title present"
);
assert!(report.warnings.is_empty(), "{:?}", report.warnings);
assert_eq!(walk(&raw).len(), raw.records.len());
}
#[test]
fn root_level_records_sit_beside_patients() {
let palette = ds(&[
(0x0008, 0x0016, VR::UI, "1.2.840.10008.5.1.4.39.1"),
(0x0008, 0x0018, VR::UI, "1.8.1"),
(0x0070, 0x0080, VR::CS, "HOT_IRON"),
]);
let entries = vec![
entry("IM1", image("P1", "1.2.1", "1.2.1.1", "1.2.1.1.1", "1")),
entry("PAL1", palette),
];
let (bytes, _) = build_dicomdir(&entries, &options()).unwrap();
let raw = parse_raw(&bytes);
let roots: Vec<String> = walk(&raw)
.iter()
.filter(|(d, _)| *d == 0)
.map(|&(_, i)| raw.records[i].record_type())
.collect();
assert_eq!(roots, ["PATIENT", "PALETTE"]);
}
#[test]
fn non_ascii_records_carry_utf8_character_set() {
let mut d = image("P1", "1.2.1", "1.2.1.1", "1.2.1.1.1", "1");
d.put(DataElement::new(
PATIENT_NAME,
VR::PN,
PrimitiveValue::from("Müller^Jürgen".to_owned()),
));
let (bytes, _) = build_dicomdir(&[entry("IM1", d)], &options()).unwrap();
let raw = parse_raw(&bytes);
let patient = &raw.records[0];
assert_eq!(patient.text(0x0008, 0x0005).unwrap(), "ISO_IR 192");
assert_eq!(patient.text(0x0010, 0x0010).unwrap(), "Müller^Jürgen");
assert!(
!raw.records[1].elements.contains_key(&(0x0008, 0x0005)),
"the ASCII STUDY record has none"
);
}
#[test]
fn copied_values_are_repadded() {
let mut d = image("P1", "1.2.1", "1.2.1.1", "1.2.1.1.1", "12\0");
d.put(DataElement::new(
Tag(0x0008, 0x1030),
VR::LO,
PrimitiveValue::from("HEAD\0".to_owned()),
));
d.put(DataElement::new(
Tag(0x0028, 0x0030),
VR::DS,
PrimitiveValue::Strs(["0.5".to_owned(), "0.5\0".to_owned()].into_iter().collect()),
));
let (bytes, _) = build_dicomdir(&[entry("IM1", d)], &options()).unwrap();
assert!(
!bytes.windows(5).any(|w| w == b"HEAD\0"),
"NUL padding from the instance is not copied"
);
let raw = parse_raw(&bytes);
assert_eq!(raw.records[1].elements[&(0x0008, 0x1030)], b"HEAD".to_vec());
assert_eq!(raw.records[3].elements[&(0x0020, 0x0013)], b"12".to_vec());
assert_eq!(
raw.records[3].elements[&(0x0028, 0x0030)],
b"0.5\\0.5 ".to_vec()
);
}
#[test]
fn file_id_rules() {
assert!(validate_file_id(&["DICOM", "ST000001", "SE000001", "IM000001"]).is_ok());
assert!(validate_file_id(&["A_1"]).is_ok());
for bad in [
vec!["dicom"],
vec!["IM1.DCM"],
vec!["TOOLONGNAME"],
vec![""],
vec!["DICOMDIR"],
vec!["A", "B", "C", "D", "E", "F", "G", "H", "I"],
vec!["SP ACE"],
] {
assert!(
matches!(
validate_file_id(&bad),
Err(DicomdirError::InvalidFileId { .. })
),
"{bad:?}"
);
}
let none: [&str; 0] = [];
assert!(validate_file_id(&none).is_err());
assert!(validate_file_set_id("SB20261114ABCD").is_ok());
assert!(validate_file_set_id("").is_ok());
assert!(validate_file_set_id("lower").is_err());
assert!(validate_file_set_id("SEVENTEEN_CHARS_X").is_err());
assert_eq!(
file_id_from_relative_path(Path::new("DICOM/IM1")).unwrap(),
["DICOM", "IM1"]
);
assert!(file_id_from_relative_path(Path::new("../IM1")).is_err());
}
#[test]
fn refuses_inconsistent_file_sets() {
let e = |id: &str, d| entry(id, d);
let err = |entries: Vec<DicomdirEntry>| build_dicomdir(&entries, &options()).unwrap_err();
assert!(matches!(err(vec![]), DicomdirError::NoEntries));
let one = image("P1", "1.2.1", "1.2.1.1", "1.2.1.1.1", "1");
assert!(matches!(
err(vec![
e("A", one.clone()),
e("A", image("P1", "1.2.1", "1.2.1.1", "1.2.1.1.2", "2"))
]),
DicomdirError::DuplicateFileId(_)
));
assert!(matches!(
err(vec![e("A", one.clone()), e("B", one.clone())]),
DicomdirError::DuplicateSopInstance { .. }
));
let mut other_name = image("P1", "1.2.9", "1.2.9.1", "1.2.9.1.1", "1");
other_name.put(DataElement::new(
PATIENT_NAME,
VR::PN,
PrimitiveValue::from("ROE^RICHARD".to_owned()),
));
assert!(matches!(
err(vec![e("A", one.clone()), e("B", other_name)]),
DicomdirError::PatientConflict { .. }
));
let study_moved = image("P2", "1.2.1", "1.2.1.5", "1.2.1.5.1", "1");
assert!(matches!(
err(vec![e("A", one.clone()), e("B", study_moved)]),
DicomdirError::StudyConflict { .. }
));
let series_moved = image("P1", "1.2.7", "1.2.1.1", "1.2.7.1.1", "1");
assert!(matches!(
err(vec![e("A", one.clone()), e("B", series_moved)]),
DicomdirError::SeriesConflict { .. }
));
let mut no_study = one.clone();
no_study.remove_element(STUDY_INSTANCE_UID);
assert!(matches!(
err(vec![e("A", no_study)]),
DicomdirError::MissingAttribute {
attribute: "StudyInstanceUID",
..
}
));
let not_an_image = ds(&[
(0x0008, 0x0016, VR::UI, "1.2.840.10008.5.1.4.1.1.200.1"),
(0x0008, 0x0018, VR::UI, "1.5.5"),
(0x0020, 0x000D, VR::UI, "1.2.1"),
(0x0020, 0x000E, VR::UI, "1.2.1.1"),
]);
assert!(matches!(
err(vec![e("A", not_an_image)]),
DicomdirError::NoRecordType { .. }
));
let bad_set = DicomdirOptions {
file_set_id: "bad id".into(),
sop_instance_uid: None,
};
assert!(matches!(
build_dicomdir(&[e("A", one)], &bad_set),
Err(DicomdirError::InvalidFileSetId { .. })
));
}
#[test]
fn entries_from_fixture_files_and_json() {
let files = [
"ct.dcm",
"mr.dcm",
"sr2.dcm",
"pdf.dcm",
"us2.dcm",
"rtdose.dcm",
];
let mut entries = Vec::new();
for (i, name) in files.iter().enumerate() {
entries.push(
DicomdirEntry::from_file(vec!["DICOM".into(), format!("IM{i:06}")], fixture(name))
.unwrap(),
);
}
let json = crate::dicom_io::dicom_file_to_json(
fixture("dx.dcm"),
crate::dicom_io::DicomJsonWriteOptions {
bulk_data_mode: crate::dicom_io::DicomJsonBulkDataMode::Uri,
..Default::default()
},
)
.unwrap();
assert!(json.contains("BulkDataURI"));
entries.push(
DicomdirEntry::from_json(
vec!["DICOM".into(), "IMJSON".into()],
&json,
DicomJsonFormat::Flat,
None,
)
.unwrap(),
);
let (bytes, report) = build_dicomdir(&entries, &options()).unwrap();
let raw = parse_raw(&bytes);
assert_eq!(walk(&raw).len(), raw.records.len());
let types: HashSet<String> = raw.records.iter().map(RawRecord::record_type).collect();
for t in [
"PATIENT",
"STUDY",
"SERIES",
"IMAGE",
"SR DOCUMENT",
"ENCAP DOC",
"RT DOSE",
] {
assert!(types.contains(t), "{t} missing from {types:?}");
}
assert!(
report
.synthesized
.iter()
.any(|s| s.attribute == "PatientID"),
"fixtures without Patient ID"
);
let leaves = raw
.records
.iter()
.filter(|r| r.elements.contains_key(&(0x0004, 0x1500)))
.count();
assert_eq!(leaves, entries.len(), "every file referenced");
}
}