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
use byteordered::Endianness;
use dicom_encoding::submit_transfer_syntax;
use dicom_encoding::transfer_syntax::{AdapterFreeTransferSyntax as Ts, Codec};
use lazy_static::lazy_static;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::fmt;
pub use dicom_encoding::TransferSyntax;
pub struct TransferSyntaxRegistry {
m: HashMap<&'static str, &'static TransferSyntax>,
}
impl fmt::Debug for TransferSyntaxRegistry {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let entries: HashMap<&str, &str> =
self.m.iter().map(|(uid, ts)| (*uid, ts.name())).collect();
f.debug_struct("TransferSyntaxRegistry")
.field("m", &entries)
.finish()
}
}
impl TransferSyntaxRegistry {
pub fn get<U: AsRef<str>>(&self, uid: U) -> Option<&'static TransferSyntax> {
let ts_uid = {
let uid = uid.as_ref();
if uid.as_bytes().last().cloned() == Some(b'\0') {
&uid[..uid.len() - 1]
} else {
&uid
}
};
self.m.get(ts_uid).copied()
}
fn register(&mut self, ts: &'static TransferSyntax) -> bool {
match self.m.entry(&ts.uid()) {
Entry::Occupied(mut e) => {
let replace = match (&e.get().codec(), ts.codec()) {
(Codec::Unsupported, Codec::Dataset(_))
| (Codec::EncapsulatedPixelData, Codec::PixelData(_)) => true,
(Codec::Unsupported, Codec::PixelData(_)) => {
eprintln!("Inconsistent requirements for transfer syntax {}: `Unsupported` cannot be replaced with `PixelData`", ts.uid());
false
}
_ => false,
};
if replace {
e.insert(ts);
true
} else {
false
}
}
Entry::Vacant(e) => {
e.insert(ts);
true
}
}
}
}
lazy_static! {
static ref REGISTRY: TransferSyntaxRegistry = {
let mut registry = TransferSyntaxRegistry {
m: HashMap::with_capacity(32),
};
for ts in inventory::iter::<TransferSyntax> {
registry.register(ts);
}
registry
};
}
pub fn get_registry() -> &'static TransferSyntaxRegistry {
®ISTRY
}
pub(crate) const fn create_ts_stub(uid: &'static str, name: &'static str) -> Ts {
TransferSyntax::new(
uid,
name,
Endianness::Little,
true,
Codec::EncapsulatedPixelData,
)
}
pub fn default() -> Ts {
IMPLICIT_VR_LITTLE_ENDIAN
}
include!("entries.rs");