1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
pub mod tags;
use crate::tags::ENTRIES;
use dicom_core::dictionary::{DataDictionary, DictionaryEntryRef, TagRange::*};
use dicom_core::header::Tag;
use lazy_static::lazy_static;
use std::collections::{HashMap, HashSet};
use std::fmt;
use std::fmt::{Display, Formatter};
lazy_static! {
static ref DICT: StandardDictionaryRegistry = init_dictionary();
}
pub fn registry() -> &'static StandardDictionaryRegistry {
&DICT
}
#[derive(Debug)]
pub struct StandardDictionaryRegistry {
by_name: HashMap<&'static str, &'static DictionaryEntryRef<'static>>,
by_tag: HashMap<Tag, &'static DictionaryEntryRef<'static>>,
repeating_ggxx: HashSet<Tag>,
repeating_eexx: HashSet<Tag>,
}
impl StandardDictionaryRegistry {
fn new() -> StandardDictionaryRegistry {
StandardDictionaryRegistry {
by_name: HashMap::with_capacity(5000),
by_tag: HashMap::with_capacity(5000),
repeating_ggxx: HashSet::with_capacity(75),
repeating_eexx: HashSet::new(),
}
}
fn index(&mut self, entry: &'static DictionaryEntryRef<'static>) -> &mut Self {
self.by_name.insert(entry.alias, entry);
self.by_tag.insert(entry.tag.inner(), entry);
match entry.tag {
Group100(tag) => {
self.repeating_ggxx.insert(tag);
}
Element100(tag) => {
self.repeating_eexx.insert(tag);
}
_ => {}
}
self
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct StandardDataDictionary;
impl StandardDataDictionary {
fn indexed_tag(tag: Tag) -> Option<&'static DictionaryEntryRef<'static>> {
let r = registry();
r.by_tag
.get(&tag)
.or_else(|| {
let group_trimmed = Tag(tag.0 & 0xFF00, tag.1);
if r.repeating_ggxx.contains(&group_trimmed) {
r.by_tag.get(&group_trimmed)
} else {
let elem_trimmed = Tag(tag.0, tag.1 & 0xFF00);
if r.repeating_eexx.contains(&elem_trimmed) {
r.by_tag.get(&elem_trimmed)
} else {
None
}
}
})
.cloned()
}
}
impl DataDictionary for StandardDataDictionary {
type Entry = DictionaryEntryRef<'static>;
fn by_name(&self, name: &str) -> Option<&Self::Entry> {
registry().by_name.get(name).cloned()
}
fn by_tag(&self, tag: Tag) -> Option<&Self::Entry> {
StandardDataDictionary::indexed_tag(tag)
}
}
impl<'a> DataDictionary for &'a StandardDataDictionary {
type Entry = DictionaryEntryRef<'static>;
fn by_name(&self, name: &str) -> Option<&'static DictionaryEntryRef<'static>> {
registry().by_name.get(name).cloned()
}
fn by_tag(&self, tag: Tag) -> Option<&'static DictionaryEntryRef<'static>> {
StandardDataDictionary::indexed_tag(tag)
}
}
impl Display for StandardDataDictionary {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
f.write_str("Standard DICOM Data Dictionary")
}
}
fn init_dictionary() -> StandardDictionaryRegistry {
let mut d = StandardDictionaryRegistry::new();
for entry in ENTRIES {
d.index(&entry);
}
d
}
#[cfg(test)]
mod tests {
use super::StandardDataDictionary;
use dicom_core::dictionary::{DataDictionary, DictionaryEntryRef, TagRange::*};
use dicom_core::header::{Tag, VR};
#[test]
fn smoke_test() {
let dict = StandardDataDictionary::default();
assert_eq!(
dict.by_name("PatientName"),
Some(&DictionaryEntryRef {
tag: Single(Tag(0x0010, 0x0010)),
alias: "PatientName",
vr: VR::PN,
})
);
assert_eq!(
dict.by_name("Modality"),
Some(&DictionaryEntryRef {
tag: Single(Tag(0x0008, 0x0060)),
alias: "Modality",
vr: VR::CS,
})
);
let pixel_data = dict
.by_tag(Tag(0x7FE0, 0x0010))
.expect("Pixel Data attribute should exist");
eprintln!("{:X?}", pixel_data.tag);
assert_eq!(pixel_data.tag, Single(Tag(0x7FE0, 0x0010)));
assert_eq!(pixel_data.alias, "PixelData");
assert!(pixel_data.vr == VR::OB || pixel_data.vr == VR::OW);
let overlay_data = dict
.by_tag(Tag(0x6000, 0x3000))
.expect("Overlay Data attribute should exist");
assert_eq!(overlay_data.tag, Group100(Tag(0x6000, 0x3000)));
assert_eq!(overlay_data.alias, "OverlayData");
assert!(overlay_data.vr == VR::OB || overlay_data.vr == VR::OW);
let overlay_data = dict
.by_tag(Tag(0x60EE, 0x3000))
.expect("Repeated Overlay Data attribute should exist");
assert_eq!(overlay_data.tag, Group100(Tag(0x6000, 0x3000)));
assert_eq!(overlay_data.alias, "OverlayData");
assert!(overlay_data.vr == VR::OB || overlay_data.vr == VR::OW);
}
#[test]
fn constants_available() {
use crate::tags::*;
assert_eq!(PATIENT_NAME, Tag(0x0010, 0x0010));
assert_eq!(MODALITY, Tag(0x0008, 0x0060));
assert_eq!(PIXEL_DATA, Tag(0x7FE0, 0x0010));
assert_eq!(STATUS, Tag(0x0000, 0x0900));
}
}