#![deny(trivial_numeric_casts, unsafe_code, unstable_features)]
#![warn(
missing_debug_implementations,
missing_docs,
unused_qualifications,
unused_import_braces
)]
use dcmnorm_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 dcmnorm_encoding::{TransferSyntax, TransferSyntaxIndex};
pub mod entries;
pub(crate) mod alloc;
mod adapters;
#[cfg(feature = "deflate")]
mod deflate;
pub struct TransferSyntaxRegistryImpl {
m: HashMap<&'static str, TransferSyntax>,
}
impl fmt::Debug for TransferSyntaxRegistryImpl {
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("TransferSyntaxRegistryImpl")
.field("m", &entries)
.finish()
}
}
impl TransferSyntaxRegistryImpl {
pub fn iter(&self) -> impl Iterator<Item = &TransferSyntax> {
self.m.values()
}
fn get<U: AsRef<str>>(&self, uid: U) -> Option<&TransferSyntax> {
let ts_uid = uid
.as_ref()
.trim_end_matches(|c: char| c.is_whitespace() || c == '\0');
self.m.get(ts_uid)
}
fn register(&mut self, ts: TransferSyntax) -> bool {
match self.m.entry(ts.uid()) {
Entry::Occupied(mut e) => {
let replace = match (&e.get().codec(), ts.codec()) {
(Codec::Dataset(None), Codec::Dataset(Some(_)))
| (
Codec::EncapsulatedPixelData(None, None),
Codec::EncapsulatedPixelData(..),
)
| (
Codec::EncapsulatedPixelData(Some(_), None),
Codec::EncapsulatedPixelData(Some(_), Some(_)),
)
| (
Codec::EncapsulatedPixelData(None, Some(_)),
Codec::EncapsulatedPixelData(Some(_), Some(_)),
) => true,
(Codec::Dataset(None), Codec::EncapsulatedPixelData(_, _)) => {
tracing::warn!("Inconsistent requirements for transfer syntax {}: `Dataset` cannot be replaced by `EncapsulatedPixelData`", ts.uid());
false
}
(Codec::EncapsulatedPixelData(_, _), Codec::Dataset(None)) => {
tracing::warn!("Inconsistent requirements for transfer syntax {}: `EncapsulatedPixelData` cannot be replaced by `Dataset`", ts.uid());
false
}
_ => false,
};
if replace {
e.insert(ts);
true
} else {
false
}
}
Entry::Vacant(e) => {
e.insert(ts);
true
}
}
}
}
impl TransferSyntaxIndex for TransferSyntaxRegistryImpl {
#[inline]
fn get(&self, uid: &str) -> Option<&TransferSyntax> {
Self::get(self, uid)
}
}
impl TransferSyntaxRegistry {
#[inline]
pub fn iter(&self) -> impl Iterator<Item = &TransferSyntax> {
get_registry().iter()
}
}
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash)]
pub struct TransferSyntaxRegistry;
impl TransferSyntaxIndex for TransferSyntaxRegistry {
#[inline]
fn get(&self, uid: &str) -> Option<&TransferSyntax> {
get_registry().get(uid)
}
}
lazy_static! {
static ref REGISTRY: TransferSyntaxRegistryImpl = {
let mut registry = TransferSyntaxRegistryImpl {
m: HashMap::with_capacity(64),
};
use self::entries::*;
let built_in_ts: [TransferSyntax; 46] = [
IMPLICIT_VR_LITTLE_ENDIAN.erased(),
EXPLICIT_VR_LITTLE_ENDIAN.erased(),
EXPLICIT_VR_BIG_ENDIAN.erased(),
ENCAPSULATED_UNCOMPRESSED_EXPLICIT_VR_LITTLE_ENDIAN.erased(),
DEFLATED_EXPLICIT_VR_LITTLE_ENDIAN.erased(),
JPIP_REFERENCED_DEFLATE.erased(),
JPIP_HTJ2K_REFERENCED_DEFLATE.erased(),
JPEG_BASELINE.erased(),
JPEG_EXTENDED.erased(),
JPEG_LOSSLESS_NON_HIERARCHICAL.erased(),
JPEG_LOSSLESS_NON_HIERARCHICAL_FIRST_ORDER_PREDICTION.erased(),
JPEG_LS_LOSSLESS_IMAGE_COMPRESSION.erased(),
JPEG_LS_LOSSY_IMAGE_COMPRESSION.erased(),
JPEG_2000_IMAGE_COMPRESSION_LOSSLESS_ONLY.erased(),
JPEG_2000_IMAGE_COMPRESSION.erased(),
JPEG_2000_PART2_MULTI_COMPONENT_IMAGE_COMPRESSION_LOSSLESS_ONLY.erased(),
JPEG_2000_PART2_MULTI_COMPONENT_IMAGE_COMPRESSION.erased(),
HIGH_THROUGHPUT_JPEG_2000_IMAGE_COMPRESSION_LOSSLESS_ONLY.erased(),
HIGH_THROUGHPUT_JPEG_2000_WITH_RPCL_OPTIONS_IMAGE_COMPRESSION_LOSSLESS_ONLY.erased(),
HIGH_THROUGHPUT_JPEG_2000_IMAGE_COMPRESSION.erased(),
JPEG_XL_LOSSLESS.erased(),
JPEG_XL_RECOMPRESSION.erased(),
JPEG_XL.erased(),
JPIP_REFERENCED.erased(),
JPIP_HTJ2K_REFERENCED.erased(),
MPEG2_MAIN_PROFILE_MAIN_LEVEL.erased(),
FRAGMENTABLE_MPEG2_MAIN_PROFILE_MAIN_LEVEL.erased(),
MPEG2_MAIN_PROFILE_HIGH_LEVEL.erased(),
FRAGMENTABLE_MPEG2_MAIN_PROFILE_HIGH_LEVEL.erased(),
MPEG4_AVC_H264_HIGH_PROFILE.erased(),
FRAGMENTABLE_MPEG4_AVC_H264_HIGH_PROFILE.erased(),
MPEG4_AVC_H264_BD_COMPATIBLE_HIGH_PROFILE.erased(),
FRAGMENTABLE_MPEG4_AVC_H264_BD_COMPATIBLE_HIGH_PROFILE.erased(),
MPEG4_AVC_H264_HIGH_PROFILE_FOR_2D_VIDEO.erased(),
FRAGMENTABLE_MPEG4_AVC_H264_HIGH_PROFILE_FOR_2D_VIDEO.erased(),
MPEG4_AVC_H264_HIGH_PROFILE_FOR_3D_VIDEO.erased(),
FRAGMENTABLE_MPEG4_AVC_H264_HIGH_PROFILE_FOR_3D_VIDEO.erased(),
MPEG4_AVC_H264_STEREO_HIGH_PROFILE.erased(),
FRAGMENTABLE_MPEG4_AVC_H264_STEREO_HIGH_PROFILE.erased(),
HEVC_H265_MAIN_PROFILE.erased(),
HEVC_H265_MAIN_10_PROFILE.erased(),
RLE_LOSSLESS.erased(),
DEFLATED_IMAGE_FRAME_COMPRESSION.erased(),
SMPTE_ST_2110_20_UNCOMPRESSED_PROGRESSIVE.erased(),
SMPTE_ST_2110_20_UNCOMPRESSED_INTERLACED.erased(),
SMPTE_ST_2110_30_PCM.erased(),
];
for ts in built_in_ts {
registry.register(ts);
}
registry
};
}
#[inline]
pub(crate) fn get_registry() -> &'static TransferSyntaxRegistryImpl {
®ISTRY
}
pub(crate) const fn create_ts_stub(uid: &'static str, name: &'static str) -> Ts {
TransferSyntax::new_ele(uid, name, Codec::EncapsulatedPixelData(None, None))
}
pub fn default() -> Ts {
entries::IMPLICIT_VR_LITTLE_ENDIAN
}
#[cfg(test)]
mod tests {
use dcmnorm_encoding::TransferSyntaxIndex;
use crate::TransferSyntaxRegistry;
#[test]
fn has_mandatory_tss() {
let implicit_vr_le = TransferSyntaxRegistry
.get("1.2.840.10008.1.2")
.expect("transfer syntax registry should provide Implicit VR Little Endian");
assert_eq!(implicit_vr_le.uid(), "1.2.840.10008.1.2");
assert!(implicit_vr_le.is_fully_supported());
let implicit_vr_le_2 = TransferSyntaxRegistry.get("1.2.840.10008.1.2\0").expect(
"transfer syntax registry should provide Implicit VR Little Endian with padded TS UID",
);
assert_eq!(implicit_vr_le_2.uid(), implicit_vr_le.uid());
let explicit_vr_le = TransferSyntaxRegistry
.get("1.2.840.10008.1.2.1")
.expect("transfer syntax registry should provide Explicit VR Little Endian");
assert_eq!(explicit_vr_le.uid(), "1.2.840.10008.1.2.1");
assert!(explicit_vr_le.is_fully_supported());
let explicit_vr_le_2 = TransferSyntaxRegistry.get("1.2.840.10008.1.2.1\0").expect(
"transfer syntax registry should provide Explicit VR Little Endian with padded TS UID",
);
assert_eq!(explicit_vr_le_2.uid(), explicit_vr_le.uid());
}
#[test]
fn provides_iter() {
let all_tss: Vec<_> = TransferSyntaxRegistry.iter().collect();
assert!(all_tss.len() >= 2);
assert!(all_tss.iter().any(|ts| ts.uid() == "1.2.840.10008.1.2"));
assert!(all_tss.iter().any(|ts| ts.uid() == "1.2.840.10008.1.2.1"));
}
}