@@ -48,7 +48,13 @@ pub use dicom_parser::error::{Error, Result};
48
48
/// The default implementation of a root DICOM object.
49
49
pub type DefaultDicomObject = RootDicomObject < mem:: InMemDicomObject < StandardDataDictionary > > ;
50
50
51
+ use std:: fs:: File ;
52
+ use std:: io:: { BufWriter , Write } ;
53
+ use std:: path:: Path ;
51
54
use dicom_core:: header:: Header ;
55
+ use dicom_encoding:: { transfer_syntax:: TransferSyntaxIndex , text:: SpecificCharacterSet } ;
56
+ use dicom_parser:: dataset:: { DataSetWriter , IntoTokens } ;
57
+ use dicom_transfer_syntax_registry:: TransferSyntaxRegistry ;
52
58
53
59
/// Trait type for a DICOM object.
54
60
/// This is a high-level abstraction where an object is accessed and
@@ -96,6 +102,39 @@ impl<T> RootDicomObject<T> {
96
102
}
97
103
}
98
104
105
+ impl < T > RootDicomObject < T >
106
+ where
107
+ for < ' a > & ' a T : IntoTokens ,
108
+ {
109
+ pub fn write_to_file < P : AsRef < Path > > ( & self , path : P ) -> Result < ( ) > {
110
+ let file = File :: create ( path) ?;
111
+ let mut to = BufWriter :: new ( file) ;
112
+
113
+ // write preamble
114
+ to. write ( & [ 0_u8 ; 128 ] [ ..] ) ?;
115
+
116
+ // write magic sequence
117
+ to. write ( b"DICM" ) ?;
118
+
119
+ // write meta group
120
+ self . meta . write ( & mut to) ?;
121
+
122
+ // prepare encoder
123
+ let registry = TransferSyntaxRegistry :: default ( ) ;
124
+ let ts = registry
125
+ . get ( & self . meta . transfer_syntax )
126
+ . ok_or_else ( || Error :: UnsupportedTransferSyntax ) ?;
127
+ let cs = SpecificCharacterSet :: Default ;
128
+ let mut dset_writer = DataSetWriter :: with_ts_cs ( to, ts, cs) ?;
129
+
130
+ // write object
131
+
132
+ dset_writer. write_sequence ( ( & self . obj ) . into_tokens ( ) ) ?;
133
+
134
+ Ok ( ( ) )
135
+ }
136
+ }
137
+
99
138
impl < T > :: std:: ops:: Deref for RootDicomObject < T > {
100
139
type Target = T ;
101
140
@@ -157,4 +196,23 @@ where
157
196
}
158
197
159
198
#[ cfg( test) ]
160
- mod tests { }
199
+ mod tests {
200
+ use crate :: RootDicomObject ;
201
+ use crate :: meta:: FileMetaTableBuilder ;
202
+
203
+ #[ test]
204
+ fn smoke_test ( ) {
205
+ let meta = FileMetaTableBuilder :: new ( )
206
+ . transfer_syntax ( dicom_transfer_syntax_registry:: entries:: EXPLICIT_VR_LITTLE_ENDIAN . uid ( ) . to_owned ( ) + "\0 " )
207
+ . media_storage_sop_class_uid ( "1.2.840.10008.5.1.4.1.1.1\0 " . to_owned ( ) )
208
+ . media_storage_sop_instance_uid ( "1.2.3.456\0 " . to_owned ( ) )
209
+ . implementation_class_uid ( "1.2.345.6.7890.1.234" . to_owned ( ) )
210
+ . build ( )
211
+ . unwrap ( ) ;
212
+ let obj = RootDicomObject :: new_empty_with_meta (
213
+ meta) ;
214
+
215
+ obj. write_to_file ( ".smoke-test.dcm" ) . unwrap ( ) ;
216
+ let _ = std:: fs:: remove_file ( ".smoke-test.dcm" ) ;
217
+ }
218
+ }
0 commit comments