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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
use std::borrow::Cow;
use std::collections::BTreeMap;
use crate::charset::Charset;
use crate::dictionary::tags;
use crate::element::Element;
use crate::endian::Endian;
use crate::error::{Error, Result};
use crate::tag::Tag;
use crate::value::Value;
use crate::valuerep::{Date, DateTime, DecimalString, IntegerString, PersonName, Time};
use crate::vr::Vr;
/// An ordered collection of data elements, corresponding to one DICOM Data Set or one Item.
///
/// Elements are stored in ascending tag order, exactly as required when writing a data set.
/// When the same tag appears twice the later one replaces the earlier; consistency warnings are
/// reported by the parser layer.
#[derive(Clone, Debug, Default)]
pub struct DataSet {
elements: BTreeMap<Tag, Element>,
endian: Endian,
/// Character set of this data set; a sequence item inherits the parent's value when it
/// carries no `(0008,0005)` of its own.
charset: Charset,
/// Tags the encoded stream carried more than once, in the order they were seen.
duplicates: Vec<Tag>,
}
impl DataSet {
/// Create an empty data set decoding numbers as little endian.
pub fn new() -> Self {
Self::default()
}
/// Create an empty data set with the given byte order (used when parsing big-endian transfer
/// syntaxes).
pub fn with_endian(endian: Endian) -> Self {
Self {
elements: BTreeMap::new(),
endian,
charset: Charset::default(),
duplicates: Vec::new(),
}
}
/// Tags the encoded stream carried more than once, in the order they were seen.
///
/// The first occurrence is kept, as pydicom does — it warns about the later ones instead of
/// reporting them. A parent data set lists the duplicates found inside its sequence items
/// too, because the parser walks them with the same reader.
pub fn duplicates(&self) -> &[Tag] {
&self.duplicates
}
pub(crate) fn set_duplicates(&mut self, duplicates: Vec<Tag>) {
self.duplicates = duplicates;
}
/// The byte order used for numbers in this data set.
pub fn endian(&self) -> Endian {
self.endian
}
/// Set the byte order. Only a parser that knows the source transfer syntax should call this.
pub fn set_endian(&mut self, endian: Endian) {
self.endian = endian;
}
/// Return a copy whose numeric values are stored in `endian`.
///
/// Text, `OB`/`UN` values and encapsulated fragments are copied unchanged; sequences are
/// converted recursively. Numeric values whose byte length is not a multiple of the VR unit
/// report [`Error::WrongLength`].
pub fn to_endian(&self, endian: Endian) -> Result<Self> {
let from = self.endian;
let mut out = self.clone();
out.endian = endian;
for element in out.elements.values_mut() {
*element = element.transcode(from, endian)?;
}
Ok(out)
}
/// Number of elements.
pub fn len(&self) -> usize {
self.elements.len()
}
/// Whether there are no elements.
pub fn is_empty(&self) -> bool {
self.elements.is_empty()
}
/// Whether the tag is present.
pub fn contains(&self, tag: Tag) -> bool {
self.elements.contains_key(&tag)
}
/// Insert an element, returning the previous element it replaced.
///
/// Inserting `(0008,0005)` updates the data set's character set; an unknown name leaves the
/// previous character set in place and reports through [`Self::charset`].
pub fn insert(&mut self, element: Element) -> Option<Element> {
if element.tag == tags::SPECIFIC_CHARACTER_SET {
if let Ok(charset) = Charset::from_element(&element) {
self.charset = charset;
}
}
self.elements.insert(element.tag, element)
}
/// Insert an element by tag, VR and value.
pub fn set(&mut self, tag: Tag, vr: Vr, value: Value) -> Option<Element> {
self.insert(Element::new(tag, vr, value))
}
/// Read an element by tag.
pub fn get(&self, tag: Tag) -> Option<&Element> {
self.elements.get(&tag)
}
/// Read an element by tag mutably.
pub fn get_mut(&mut self, tag: Tag) -> Option<&mut Element> {
self.elements.get_mut(&tag)
}
/// Remove an element.
pub fn remove(&mut self, tag: Tag) -> Option<Element> {
self.elements.remove(&tag)
}
/// Iterate elements in ascending tag order.
pub fn iter(&self) -> impl Iterator<Item = &Element> + '_ {
self.elements.values()
}
/// Iterate elements mutably in ascending tag order.
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Element> + '_ {
self.elements.values_mut()
}
/// Iterate all tags in ascending order.
pub fn tags(&self) -> impl Iterator<Item = Tag> + '_ {
self.elements.keys().copied()
}
/// Read an element by tag, returning [`Error::MissingTag`] when absent.
pub fn element(&self, tag: Tag) -> Result<&Element> {
self.get(tag).ok_or(Error::MissingTag(tag))
}
/// The character set declared by `(0008,0005)`, or the value inherited from the parent data
/// set when the element is absent.
pub fn charset(&self) -> Result<Charset> {
match self.get(tags::SPECIFIC_CHARACTER_SET) {
Some(element) => Charset::from_element(element),
None => Ok(self.charset),
}
}
/// Set the character set used when `(0008,0005)` is absent. Parsers use this to hand a
/// sequence item the character set of its parent.
pub fn set_charset(&mut self, charset: Charset) {
self.charset = charset;
}
/// Read the first string value, decoded with the data set's character set.
pub fn string(&self, tag: Tag) -> Result<Cow<'_, str>> {
let charset = self.charset()?;
self.element(tag)?.string_with(charset)
}
/// Read all string values, decoded with the data set's character set.
pub fn strings(&self, tag: Tag) -> Result<Vec<Cow<'_, str>>> {
let charset = self.charset()?;
self.element(tag)?.string_values_with(charset)
}
/// Read a single 16-bit unsigned integer.
pub fn u16(&self, tag: Tag) -> Result<u16> {
self.u16s(tag)?.first().copied().ok_or(Error::NoValue(tag))
}
/// Read all 16-bit unsigned integer values.
pub fn u16s(&self, tag: Tag) -> Result<Vec<u16>> {
self.element(tag)?.u16s(self.endian)
}
/// Read all 32-bit signed integer values.
pub fn i32s(&self, tag: Tag) -> Result<Vec<i32>> {
self.element(tag)?.i32s(self.endian)
}
/// Read all double-precision float values.
pub fn f64s(&self, tag: Tag) -> Result<Vec<f64>> {
self.element(tag)?.f64s(self.endian)
}
/// Read all numeric values of `DS` text.
pub fn decimal_values(&self, tag: Tag) -> Result<Vec<f64>> {
self.element(tag)?.decimal_values()
}
/// Read the first person name (`PN`), decoded with the data set's character set.
pub fn person_name(&self, tag: Tag) -> Result<PersonName> {
let element = self.element(tag)?;
require_vr(element, Vr::Pn, "PN")?;
let values = self.strings(tag)?;
let text = values.first().ok_or(Error::NoValue(tag))?;
PersonName::parse(text)
}
/// Read every decimal string (`DS`), keeping the text each value was written as.
pub fn decimal_strings(&self, tag: Tag) -> Result<Vec<DecimalString>> {
let element = self.element(tag)?;
require_vr(element, Vr::Ds, "DS")?;
let mut values = Vec::new();
for part in self.strings(tag)? {
if part.trim().is_empty() {
continue;
}
values.push(DecimalString::parse(&part)?);
}
Ok(values)
}
/// Read every integer string (`IS`), keeping the text each value was written as.
pub fn integer_strings(&self, tag: Tag) -> Result<Vec<IntegerString>> {
let element = self.element(tag)?;
require_vr(element, Vr::Is, "IS")?;
let mut values = Vec::new();
for part in self.strings(tag)? {
if part.trim().is_empty() {
continue;
}
values.push(IntegerString::parse(&part)?);
}
Ok(values)
}
/// Read the first date (`DA`).
pub fn date(&self, tag: Tag) -> Result<Date> {
let element = self.element(tag)?;
require_vr(element, Vr::Da, "DA")?;
let values = self.strings(tag)?;
let text = values.first().ok_or(Error::NoValue(tag))?;
Date::parse(text)
}
/// Read the first time (`TM`).
pub fn time(&self, tag: Tag) -> Result<Time> {
let element = self.element(tag)?;
require_vr(element, Vr::Tm, "TM")?;
let values = self.strings(tag)?;
let text = values.first().ok_or(Error::NoValue(tag))?;
Time::parse(text)
}
/// Read the first date-time (`DT`).
pub fn date_time(&self, tag: Tag) -> Result<DateTime> {
let element = self.element(tag)?;
require_vr(element, Vr::Dt, "DT")?;
let values = self.strings(tag)?;
let text = values.first().ok_or(Error::NoValue(tag))?;
DateTime::parse(text)
}
/// Read sequence items.
pub fn sequence(&self, tag: Tag) -> Result<&[DataSet]> {
self.element(tag)?.as_sequence()
}
/// Write a string value.
pub fn set_string(&mut self, tag: Tag, vr: Vr, text: &str) {
self.set(tag, vr, Value::from_str(text));
}
/// Write a single 16-bit unsigned integer (`US`).
pub fn set_u16(&mut self, tag: Tag, value: u16) {
self.set(tag, Vr::Us, Value::from_u16s(&[value]));
}
/// Write a sequence.
pub fn set_sequence(&mut self, tag: Tag, items: Vec<DataSet>) {
self.set(tag, Vr::Sq, Value::sequence(items));
}
}
/// Two data sets are equal when they hold the same elements.
///
/// The byte order and the inherited character set are reading state rather than content, so they
/// do not take part: the same numbers in another byte order compare equal here, even though they
/// decode differently until [`DataSet::to_endian`] makes the two agree.
impl PartialEq for DataSet {
fn eq(&self, other: &Self) -> bool {
self.elements == other.elements
}
}
impl Eq for DataSet {}
/// Reject an element whose VR is not the one the accessor expects.
fn require_vr(element: &Element, vr: Vr, name: &'static str) -> Result<()> {
if element.vr != vr {
return Err(Error::WrongVr {
actual: element.vr,
expected: name,
});
}
Ok(())
}