Skip to main content

dicom_toolkit_data/io/
transfer.rs

1//! Transfer syntax properties helpers.
2
3use dicom_toolkit_dict::ts::{transfer_syntaxes, ByteOrder, PixelEncoding, VrEncoding};
4use dicom_toolkit_dict::{tags, Tag, Vr};
5
6/// Properties of a transfer syntax relevant to encoding/decoding.
7pub struct TransferSyntaxProperties {
8    pub byte_order: ByteOrder,
9    pub vr_encoding: VrEncoding,
10    pub is_deflated: bool,
11    pub is_encapsulated: bool,
12}
13
14impl TransferSyntaxProperties {
15    pub fn from_uid(uid: &str) -> Self {
16        if let Some(ts) = transfer_syntaxes::by_uid(uid) {
17            Self {
18                byte_order: ts.byte_order,
19                vr_encoding: ts.vr_encoding,
20                is_deflated: ts.deflated,
21                is_encapsulated: ts.pixel_encoding == PixelEncoding::Encapsulated,
22            }
23        } else {
24            // Unknown TS → Explicit VR LE (safest default)
25            Self {
26                byte_order: ByteOrder::LittleEndian,
27                vr_encoding: VrEncoding::Explicit,
28                is_deflated: false,
29                is_encapsulated: false,
30            }
31        }
32    }
33
34    pub fn is_little_endian(&self) -> bool {
35        self.byte_order == ByteOrder::LittleEndian
36    }
37
38    pub fn is_explicit_vr(&self) -> bool {
39        self.vr_encoding == VrEncoding::Explicit
40    }
41}
42
43/// Infer the VR for a tag in Implicit VR transfer syntaxes.
44/// Returns `Vr::UN` for unknown tags.
45pub fn implicit_vr_for_tag(tag: Tag) -> Vr {
46    match tag {
47        // File meta (group 0002)
48        tags::FILE_META_INFORMATION_GROUP_LENGTH => Vr::UL,
49        tags::FILE_META_INFORMATION_VERSION => Vr::OB,
50        tags::MEDIA_STORAGE_SOP_CLASS_UID => Vr::UI,
51        tags::MEDIA_STORAGE_SOP_INSTANCE_UID => Vr::UI,
52        tags::TRANSFER_SYNTAX_UID => Vr::UI,
53        tags::IMPLEMENTATION_CLASS_UID => Vr::UI,
54        tags::IMPLEMENTATION_VERSION_NAME => Vr::SH,
55        // General
56        tags::SPECIFIC_CHARACTER_SET => Vr::CS,
57        tags::IMAGE_TYPE => Vr::CS,
58        tags::SOP_CLASS_UID => Vr::UI,
59        tags::SOP_INSTANCE_UID => Vr::UI,
60        tags::STUDY_DATE => Vr::DA,
61        tags::SERIES_DATE => Vr::DA,
62        tags::ACQUISITION_DATE => Vr::DA,
63        tags::CONTENT_DATE => Vr::DA,
64        tags::STUDY_TIME => Vr::TM,
65        tags::SERIES_TIME => Vr::TM,
66        tags::ACQUISITION_TIME => Vr::TM,
67        tags::CONTENT_TIME => Vr::TM,
68        tags::ACCESSION_NUMBER => Vr::SH,
69        tags::MODALITY => Vr::CS,
70        tags::MANUFACTURER => Vr::LO,
71        tags::INSTITUTION_NAME => Vr::LO,
72        tags::REFERRING_PHYSICIAN_NAME => Vr::PN,
73        tags::STUDY_DESCRIPTION => Vr::LO,
74        tags::SERIES_DESCRIPTION => Vr::LO,
75        tags::PERFORMING_PHYSICIAN_NAME => Vr::PN,
76        tags::OPERATORS_NAME => Vr::PN,
77        tags::REFERENCED_SOP_CLASS_UID => Vr::UI,
78        tags::REFERENCED_SOP_INSTANCE_UID => Vr::UI,
79        // Patient
80        tags::PATIENT_NAME => Vr::PN,
81        tags::PATIENT_ID => Vr::LO,
82        tags::PATIENT_BIRTH_DATE => Vr::DA,
83        tags::PATIENT_SEX => Vr::CS,
84        tags::PATIENT_AGE => Vr::AS,
85        tags::PATIENT_SIZE => Vr::DS,
86        tags::PATIENT_WEIGHT => Vr::DS,
87        // Study / Series
88        tags::STUDY_INSTANCE_UID => Vr::UI,
89        tags::SERIES_INSTANCE_UID => Vr::UI,
90        tags::STUDY_ID => Vr::SH,
91        tags::SERIES_NUMBER => Vr::IS,
92        tags::ACQUISITION_NUMBER => Vr::IS,
93        tags::INSTANCE_NUMBER => Vr::IS,
94        tags::IMAGE_POSITION_PATIENT => Vr::DS,
95        tags::IMAGE_ORIENTATION_PATIENT => Vr::DS,
96        tags::FRAME_OF_REFERENCE_UID => Vr::UI,
97        tags::SLICE_LOCATION => Vr::DS,
98        tags::NUMBER_OF_FRAMES => Vr::IS,
99        // Image
100        tags::SAMPLES_PER_PIXEL => Vr::US,
101        tags::PHOTOMETRIC_INTERPRETATION => Vr::CS,
102        tags::ROWS => Vr::US,
103        tags::COLUMNS => Vr::US,
104        tags::BITS_ALLOCATED => Vr::US,
105        tags::BITS_STORED => Vr::US,
106        tags::HIGH_BIT => Vr::US,
107        tags::PIXEL_REPRESENTATION => Vr::US,
108        tags::PLANAR_CONFIGURATION => Vr::US,
109        tags::PIXEL_DATA => Vr::OW,
110        // Window/Rescale
111        tags::WINDOW_CENTER => Vr::DS,
112        tags::WINDOW_WIDTH => Vr::DS,
113        tags::RESCALE_INTERCEPT => Vr::DS,
114        tags::RESCALE_SLOPE => Vr::DS,
115        _ => Vr::UN,
116    }
117}