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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
//! Explicit VR Big Endian syntax transfer implementation.
use crate::encode::basic::BigEndianBasicEncoder;
use crate::encode::{
BasicEncode, Encode, Result, WriteHeaderSnafu, WriteItemDelimiterSnafu, WriteItemHeaderSnafu,
WriteOffsetTableSnafu, WriteSequenceDelimiterSnafu, WriteTagSnafu,
};
use byteordered::byteorder::{BigEndian, ByteOrder};
use byteordered::Endianness;
use dicom_core::header::{DataElementHeader, HasLength, Header};
use dicom_core::{PrimitiveValue, Tag, VR};
use snafu::ResultExt;
use std::io::{self, Write};
/// A concrete encoder for the transfer syntax ExplicitVRBigEndian
#[derive(Debug, Default, Clone)]
pub struct ExplicitVRBigEndianEncoder {
basic: BigEndianBasicEncoder,
}
impl BasicEncode for ExplicitVRBigEndianEncoder {
fn endianness(&self) -> Endianness {
Endianness::Big
}
fn encode_us<S>(&self, to: S, value: u16) -> io::Result<()>
where
S: Write,
{
self.basic.encode_us(to, value)
}
fn encode_ul<S>(&self, to: S, value: u32) -> io::Result<()>
where
S: Write,
{
self.basic.encode_ul(to, value)
}
fn encode_uv<S>(&self, to: S, value: u64) -> io::Result<()>
where
S: Write,
{
self.basic.encode_uv(to, value)
}
fn encode_ss<S>(&self, to: S, value: i16) -> io::Result<()>
where
S: Write,
{
self.basic.encode_ss(to, value)
}
fn encode_sl<S>(&self, to: S, value: i32) -> io::Result<()>
where
S: Write,
{
self.basic.encode_sl(to, value)
}
fn encode_sv<S>(&self, to: S, value: i64) -> io::Result<()>
where
S: Write,
{
self.basic.encode_sv(to, value)
}
fn encode_fl<S>(&self, to: S, value: f32) -> io::Result<()>
where
S: Write,
{
self.basic.encode_fl(to, value)
}
fn encode_fd<S>(&self, to: S, value: f64) -> io::Result<()>
where
S: Write,
{
self.basic.encode_fd(to, value)
}
}
impl Encode for ExplicitVRBigEndianEncoder {
fn encode_tag<W>(&self, mut to: W, tag: Tag) -> Result<()>
where
W: Write,
{
let mut buf = [0u8, 4];
BigEndian::write_u16(&mut buf[..], tag.group());
BigEndian::write_u16(&mut buf[2..], tag.element());
to.write_all(&buf).context(WriteTagSnafu)
}
fn encode_element_header<W>(&self, mut to: W, de: DataElementHeader) -> Result<usize>
where
W: Write,
{
match de.vr() {
// PS3.5 7.1.2:
// for VRs of AE, AS, AT, CS, DA, DS, DT, FL, FD, IS, LO, LT, PN,
// SH, SL, SS, ST, TM, UI, UL and US the Value Length Field is the
// 16-bit unsigned integer following the two byte VR Field (Table
// 7.1-2). The value of the Value Length Field shall equal the
// length of the Value Field.
VR::AE
| VR::AS
| VR::AT
| VR::CS
| VR::DA
| VR::DS
| VR::DT
| VR::FL
| VR::FD
| VR::IS
| VR::LO
| VR::LT
| VR::PN
| VR::SH
| VR::SL
| VR::SS
| VR::ST
| VR::TM
| VR::UI
| VR::UL
| VR::US => {
let mut buf = [0u8; 8];
BigEndian::write_u16(&mut buf[0..], de.tag().group());
BigEndian::write_u16(&mut buf[2..], de.tag().element());
let vr_bytes = de.vr().to_bytes();
buf[4] = vr_bytes[0];
buf[5] = vr_bytes[1];
BigEndian::write_u16(&mut buf[6..], de.length().0 as u16);
to.write_all(&buf).context(WriteHeaderSnafu)?;
Ok(8)
}
// PS3.5 7.1.2:
// for all other VRs the 16 bits following the two byte VR Field
// are reserved for use by later versions of the DICOM Standard.
// These reserved bytes shall be set to 0000H and shall not be
// used or decoded (Table 7.1-1). The Value Length Field is a
// 32-bit unsigned integer.
_ => {
let mut buf = [0u8; 12];
BigEndian::write_u16(&mut buf[0..], de.tag().group());
BigEndian::write_u16(&mut buf[2..], de.tag().element());
let vr_bytes = de.vr().to_bytes();
buf[4] = vr_bytes[0];
buf[5] = vr_bytes[1];
// buf[6..8] is kept zero'd
BigEndian::write_u32(&mut buf[8..], de.length().0);
to.write_all(&buf).context(WriteHeaderSnafu)?;
Ok(12)
}
}
}
fn encode_item_header<W>(&self, mut to: W, len: u32) -> Result<()>
where
W: Write,
{
let mut buf = [0u8; 8];
BigEndian::write_u16(&mut buf, 0xFFFE);
BigEndian::write_u16(&mut buf[2..], 0xE000);
BigEndian::write_u32(&mut buf[4..], len);
to.write_all(&buf).context(WriteItemHeaderSnafu)
}
fn encode_item_delimiter<W>(&self, mut to: W) -> Result<()>
where
W: Write,
{
let mut buf = [0u8; 8];
BigEndian::write_u16(&mut buf, 0xFFFE);
BigEndian::write_u16(&mut buf[2..], 0xE00D);
// remaining bytes are already zero, so it's ready to write
to.write_all(&buf).context(WriteItemDelimiterSnafu)
}
fn encode_sequence_delimiter<W>(&self, mut to: W) -> Result<()>
where
W: Write,
{
let mut buf = [0u8; 8];
BigEndian::write_u16(&mut buf, 0xFFFE);
BigEndian::write_u16(&mut buf[2..], 0xE0DD);
// remaining bytes are already zero, so it's ready to write
to.write_all(&buf).context(WriteSequenceDelimiterSnafu)
}
fn encode_primitive<W>(&self, to: W, value: &PrimitiveValue) -> Result<usize>
where
W: Write,
{
self.basic.encode_primitive(to, value)
}
fn encode_offset_table<W>(&self, mut to: W, offset_table: &[u32]) -> Result<usize>
where
W: Write,
{
for v in offset_table {
self.basic
.encode_ul(&mut to, *v)
.context(WriteOffsetTableSnafu)?;
}
Ok(offset_table.len() * 4)
}
}
#[cfg(test)]
mod tests {
use super::ExplicitVRBigEndianEncoder;
use crate::encode::Encode;
use dicom_core::header::{DataElementHeader, Length};
use dicom_core::{Tag, VR};
use std::io::{Cursor, Write};
type Result = std::result::Result<(), Box<dyn std::error::Error>>;
// manually crafting some DICOM data elements
#[rustfmt::skip]
const RAW: &[u8] = &[
0x00, 0x02, 0x00, 0x02, // (0002,0002) (BE) Media Storage SOP Class UID
b'U', b'I', // VR: UI (UID)
0x00, 0x1A, // Length: 26 bytes (BE)
// UID: 1.2.840.10008.5.1.4.1.1.1
b'1', b'.', b'2', b'.', b'8', b'4', b'0', b'.', b'1', b'0', b'0', b'0', b'8', b'.',
b'5', b'.', b'1', b'.', b'4', b'.', b'1', b'.', b'1', b'.', b'1',
0x00, // Padding to make length even
0x00, 0x02, 0x00, 0x10, // (0002,0010) (BE) Transfer Syntax UID
b'U', b'I', // VR: UI (UID)
0x00, 0x14, // Length: 20 bytes (BE)
// UID: 1.2.840.10008.1.2.1 (ExplicitVRLittleEndian)
b'1', b'.', b'2', b'.', b'8', b'4', b'0', b'.', b'1', b'0', b'0', b'0', b'8', b'.',
b'1', b'.', b'2', b'.', b'1',
0x00, // Padding to make length even
0x00, 0x08, 0x00, 0x54, // (0008,0054) (BE) Retrieve AE Title
b'A', b'E', // VR: AE (Application Entity)
0x00, 0x06, // Length: 6 bytes (BE)
// String "TITLE"
b'T', b'I', b'T', b'L', b'E',
b' ', // Padding to make length even
0x00, 0x10, 0x10, 0x10, // (0010,1010) (BE) Patient's Age
b'A', b'S', // VR: AS (Age String)
0x00, 0x02, // Length: 2 bytes (BE)
// String "8Y"
b'8', b'Y',
0x00, 0x72, 0x00, 0x26, // (0072,0026) (BE) Selector Attribute
b'A', b'T', // VR: AT (Attribute Tag)
0x00, 0x04, // Length: 4 bytes (BE)
// Tag: (0028,2110) (BE) Lossy Image Compression
0x00, 0x28, 0x21, 0x10,
0x00, 0x10, 0x00, 0x40, // (0010,0040) (BE) Patient Sex
b'C', b'S', // VR: CS (Code String)
0x00, 0x02, // Length: 2 bytes (BE)
// Text: "O"
b'O',
b' ', // Padding to make length even
0x00, 0x10, 0x00, 0x30, // (0010,0030) (BE) Patient's Birth Date
b'D', b'A', // VR: DA (Date)
0x00, 0x08, // Length: 8 bytes (BE)
// String "19800101"
b'1', b'9', b'8', b'0', b'0', b'1', b'0', b'1',
0x00, 0x10, 0x10, 0x20, // (0010,1020) (BE) Patient's size
b'D', b'S', // VR: DS (Decimal String)
0x00, 0x04, // Length: 4 bytes (BE)
// String: "1.70"
b'1', b'.', b'7', b'0',
0x00, 0x08, 0x00, 0x15, // (0008,0015) (BE) Instance coercion datetime
b'D', b'T', // VR: DT (Date Time)
0x00, 0x0E, // Length: 14 bytes (BE)
// String "20051231235960"
b'2', b'0', b'0', b'5', b'1', b'2', b'3', b'1', b'2', b'3', b'5', b'9', b'6', b'0',
0x00, 0x10, 0x94, 0x31, // (0010,9431) (BE) Examined Body Thickness
b'F', b'L', // VR: FL (Floating-point IEEE-754)
0x00, 0x04, // Length: 4 bytes (BE)
// Value: 3.1415927 (BE)
0x40, 0x49, 0x0F, 0xDB,
0x00, 0x40, 0x92, 0x25, // (0040,9225) (BE) Real World Value Slope
b'F', b'D', // VR: FD (Double-precision floating-point IEEE-754)
0x00, 0x08, // Length: 8 bytes (BE)
// Value: 3.141592653589793 (BE)
0x40, 0x09, 0x21, 0xFB, 0x54, 0x44, 0x2D, 0x18,
0x00, 0x20, 0x00, 0x13, // (0020,0013) (BE) Instance Number
b'I', b'S', // VR: IS (Integer String)
0x00, 0x08, // Length: 8 bytes (BE)
// String: "1234567"
b'1', b'2', b'3', b'4', b'5', b'6', b'7',
b' ', // Padding to make length even
0x00, 0x10, 0x00, 0x20, // (0010,0020) (BE) Patient ID
b'L', b'O', // VR: LO (Long string)
0x00, 0x0A, // Length: 10 bytes (BE)
// String "P12345678X"
b'P', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'X',
0x00, 0x10, 0x40, 0x00, // (0010,4000) (BE) Patient Comments
b'L', b'T', // VR: LT (Long Text)
0x00, 0x04, // Length: 4 bytes (BE)
// String "None"
b'N', b'o', b'n', b'e',
0x00, 0x08, 0x04, 0x1B, // (0008,041B) (BE) RecordKey
b'O', b'B', // VR: OB (Other Byte)
0x00, 0x00, // Reserved, always 0
0x00, 0x00, 0x00, 0x02, // Length: 2 bytes (BE)
// Value: [0x12, 0x34]
0x12, 0x34,
0x7F, 0xE0, 0x00, 0x09, // (7FE0,0009) (BE) Double Float Pixel Data
b'O', b'D', // VR: OD (Other Double)
0x00, 0x00, // Reserved, always 0
0x00, 0x00, 0x00, 0x08, // Length: 8 bytes (BE)
// Value: [3.141592653589793] (BE)
0x40, 0x09, 0x21, 0xFB, 0x54, 0x44, 0x2D, 0x18,
0x7F, 0xE0, 0x00, 0x08, // (7FE0,0008) (BE) Float Pixel Data
b'O', b'F', // VR: OF (Other Float)
0x00, 0x00, // Reserved, always 0
0x00, 0x00, 0x00, 0x04, // Length: 4 bytes (BE)
// Value: [3.1415927] (BE)
0x40, 0x49, 0x0F, 0xDB,
0x00, 0x72, 0x00, 0x75, // (0072,0075) (BE) Selector OL Value
b'O', b'L', // VR: OL (Other Long)
0x00, 0x00, // Reserved, always 0
0x00, 0x00, 0x00, 0x04, // Length: 4 bytes (BE)
// Value: [0x12345678] (BE)
0x12, 0x34, 0x56, 0x78,
0x00, 0x72, 0x00, 0x81, // (0072,0081) (BE) Selector OV Value
b'O', b'V', // VR: OV (Other Very long)
0x00, 0x00, // Reserved, always 0
0x00, 0x00, 0x00, 0x08, // Length: 8 bytes (BE)
// Value: [0x192A3B4C5D6E7F80] (BE)
0x19, 0x2A, 0x3B, 0x4C, 0x5D, 0x6E, 0x7F, 0x80,
0x00, 0x72, 0x00, 0x69, // (0072,0069) (BE) Selector OW Value
b'O', b'W', // VR: OW (Other Word)
0x00, 0x00, // Reserved, always 0
0x00, 0x00, 0x00, 0x02, // Length: 2 bytes (BE)
// Value: [0x1234] (BE)
0x12, 0x34,
0x00, 0x10, 0x00, 0x10, // (0010,0010) (BE) Patient Name
b'P', b'N', // VR: PN (Person Name)
0x00, 0x08, // Length: 8 bytes (BE)
// String: "Doe^John"
b'D', b'o', b'e', b'^', b'J', b'o', b'h', b'n',
0x00, 0x40, 0x92, 0x10, // (0040,9210) (BE) LUT Label
b'S', b'H', // VR: SH (Short string)
0x00, 0x04, // Length: 4 bytes (BE)
// String: "LBL"
b'L', b'B', b'L',
b' ', // Padding to make length even
0x00, 0x18, 0x60, 0x20, // (0018,6020) (BE) Reference Pixel X0
b'S', b'L', // VR: SL (Signed Long)
0x00, 0x04, // Length: 4 bytes (BE)
// Value: -12345678 (BE)
0xFF, 0x43, 0x9E, 0xB2,
// Sequences (VR: SQ) tested elsewhere
0x00, 0x28, 0x95, 0x03, // (0028,9503) (BE) Vertices of the Region (VM 2-2n)
b'S', b'S', // VR: SS (Signed Short)
0x00, 0x04, // Length: 4 bytes (2 * 2) (BE)
// Value: -4567 (BE)
0xEE, 0x29,
// Value: 4321 (BE)
0x10, 0xE1,
0x00, 0x40, 0x02, 0x80, // (0040,0280) (BE) Comments on the Performed Procedure Step
b'S', b'T', // VR: ST (Short Text)
0x00, 0x0A, // Length: 10 bytes (BE)
// String: "No comment"
b'N', b'o', b' ', b'c', b'o', b'm', b'm', b'e', b'n', b't',
0x00, 0x72, 0x00, 0x82, // (0072,0082) (BE) SelectorSVValue (VM: 1-n)
b'S', b'V', // VR: SV (Signed Very long)
0x00, 0x00, // Reserved, always 0
0x00, 0x00, 0x00, 0x10, // Length: 16 bytes (2 * 8) (BE)
// Value: -123456789012345678 (BE)
0xFE, 0x49, 0x64, 0xB4, 0x59, 0xCF, 0x0C, 0xB2,
// Value: 123456789012345678 (BE)
0x01, 0xB6, 0x9B, 0x4B, 0xA6, 0x30, 0xF3, 0x4E,
0x00, 0x10, 0x00, 0x32, // (0010,0032) (BE) Patient's Birth Time
b'T', b'M', // VR: TM (Time)
0x00, 0x06, // Length: 6 bytes (BE)
// String: "123456"
b'1', b'2', b'3', b'4', b'5', b'6',
0x00, 0x08, 0x01, 0x19, // (0008,0119) (BE) Long Code Value
b'U', b'C', // VR: UC (Unlimited Characters)
0x00, 0x00, // Reserved, always 0
0x00, 0x00, 0x00, 0x04, // Length: 4 bytes (BE)
// String: "Code"
b'C', b'o', b'd', b'e',
// UI already tested above
0x00, 0x18, 0x60, 0x16, // (0018,6016) (BE) Region Flags
b'U', b'L',
0x00, 0x04, // Length: 4 bytes (BE)
// Value: 1 (BE)
0x00, 0x00, 0x00, 0x01,
0xC0, 0x01, 0x12, 0x34, // (C001,1234) (BE) (Private data element)
b'U', b'N', // VR: UN (Unknown)
0x00, 0x00, // Reserved, always 0
0x00, 0x00, 0x00, 0x06, // Length: 6 bytes (BE)
// Value: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06]
0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
0x00, 0x08, 0x01, 0x0E, // (0008,010E) (BE) Coding Scheme URL
b'U', b'R', // VR: UR (Universal Resource Locator, URL)
0x00, 0x00, // Reserved, always 0
0x00, 0x00, 0x00, 0x12, // Length: 18 bytes (BE)
// String: "http://example.com"
b'h', b't', b't', b'p', b':', b'/', b'/', b'e', b'x', b'a', b'm', b'p', b'l', b'e',
b'.', b'c', b'o', b'm',
0x00, 0x08, 0x00, 0x40, // (0008,0040) (BE) Data Set Type
b'U', b'S', // VR: US (Unsigned Short)
0x00, 0x02, // Length: 2 bytes (BE)
// Value: 34567 (BE)
0x87, 0x07,
0x00, 0x18, 0x99, 0x17, // (0018,9917) (BE) Instruction Description
b'U', b'T', // VR: UT (Unlimited Text)
0x00, 0x00, // Reserved, always 0
0x00, 0x00, 0x00, 0x08, // Length: 8 bytes (BE)
// String: "No text"
b'N', b'o', b' ', b't', b'e', b'x', b't',
b' ', // Padding to make length even
0x00, 0x08, 0x04, 0x0C, // (0008,040C) (BE) File Offset in Container
b'U', b'V', // VR: UV (Unsigned Very long)
0x00, 0x00, // Reserved, always 0
0x00, 0x00, 0x00, 0x08, // Length: 8 bytes (BE)
// Value: 12345678901234567890 (BE)
0xAB, 0x54, 0xA9, 0x8C, 0xEB, 0x1F, 0x0A, 0xD2,
];
#[test]
fn encode_explicit_vr_be() {
let mut buf = vec![0u8; RAW.len()];
let enc = ExplicitVRBigEndianEncoder::default();
let mut writer = Cursor::new(&mut buf);
// encode first element
let de = DataElementHeader::new(Tag(0x0002, 0x0002), VR::UI, Length(26));
let len = enc
.encode_element_header(&mut writer, de)
.expect("should write it fine");
assert_eq!(len, 8);
writer
.write_all(b"1.2.840.10008.5.1.4.1.1.1\0".as_ref())
.expect("should write the value fine");
// encode second element
let de = DataElementHeader::new(Tag(0x0002, 0x0010), VR::UI, Length(20));
let len = enc
.encode_element_header(&mut writer, de)
.expect("should write it fine");
assert_eq!(len, 8);
writer
.write_all(b"1.2.840.10008.1.2.1\0".as_ref())
.expect("should write the value fine");
fn write_elem(
enc: &ExplicitVRBigEndianEncoder,
mut writer: &mut Cursor<&mut Vec<u8>>,
group: u16,
element: u16,
vr: VR,
value: &[u8],
) {
let from = writer.position() as usize;
let de = DataElementHeader::new(
Tag(group, element),
vr,
Length(value.len() as u32),
);
let _written_len = enc
.encode_element_header(&mut writer, de)
.expect("should write it fine");
writer.write_all(value).expect("should write the value fine");
let to = writer.position() as usize;
// Compare the current slice
if &writer.get_ref()[from..to] != &RAW[from..to] {
panic!(
"Failure on ({:04x},{:04x}) {:?} {:02x?}\n\
Expected: {:02x?}",
group,
element,
vr,
value,
&RAW[from..to],
);
}
}
write_elem(&enc, &mut writer, 0x0008, 0x0054, VR::AE, b"TITLE ");
write_elem(&enc, &mut writer, 0x0010, 0x1010, VR::AS, b"8Y");
write_elem(
&enc,
&mut writer,
0x0072,
0x0026,
VR::AT,
&[0x00, 0x28, 0x21, 0x10],
);
write_elem(&enc, &mut writer, 0x0010, 0x0040, VR::CS, b"O ");
write_elem(&enc, &mut writer, 0x0010, 0x0030, VR::DA, b"19800101");
write_elem(&enc, &mut writer, 0x0010, 0x1020, VR::DS, b"1.70");
write_elem(
&enc,
&mut writer,
0x0008,
0x0015,
VR::DT,
b"20051231235960",
);
write_elem(
&enc,
&mut writer,
0x0010,
0x9431,
VR::FL,
&[0x40, 0x49, 0x0F, 0xDB],
);
write_elem(
&enc,
&mut writer,
0x0040,
0x9225,
VR::FD,
&[0x40, 0x09, 0x21, 0xFB, 0x54, 0x44, 0x2D, 0x18],
);
write_elem(&enc, &mut writer, 0x0020, 0x0013, VR::IS, b"1234567 ");
write_elem(&enc, &mut writer, 0x0010, 0x0020, VR::LO, b"P12345678X");
write_elem(&enc, &mut writer, 0x0010, 0x4000, VR::LT, b"None");
write_elem(&enc, &mut writer, 0x0008, 0x041B, VR::OB, &[0x12, 0x34]);
write_elem(
&enc,
&mut writer,
0x7FE0,
0x0009,
VR::OD,
&[0x40, 0x09, 0x21, 0xFB, 0x54, 0x44, 0x2D, 0x18],
);
write_elem(
&enc,
&mut writer,
0x7FE0,
0x0008,
VR::OF,
&[0x40, 0x49, 0x0F, 0xDB],
);
write_elem(
&enc,
&mut writer,
0x0072,
0x0075,
VR::OL,
&[0x12, 0x34, 0x56, 0x78],
);
write_elem(
&enc,
&mut writer,
0x0072,
0x0081,
VR::OV,
&[0x19, 0x2A, 0x3B, 0x4C, 0x5D, 0x6E, 0x7F, 0x80],
);
write_elem(&enc, &mut writer, 0x0072, 0x0069, VR::OW, &[0x12, 0x34]);
write_elem(&enc, &mut writer, 0x0010, 0x0010, VR::PN, b"Doe^John");
write_elem(&enc, &mut writer, 0x0040, 0x9210, VR::SH, b"LBL ");
write_elem(
&enc,
&mut writer,
0x0018,
0x6020,
VR::SL,
&[0xFF, 0x43, 0x9E, 0xB2],
);
write_elem(
&enc,
&mut writer,
0x0028,
0x9503,
VR::SS,
&[0xEE, 0x29, 0x10, 0xE1],
);
write_elem(&enc, &mut writer, 0x0040, 0x0280, VR::ST, b"No comment");
write_elem(
&enc,
&mut writer,
0x0072,
0x0082,
VR::SV,
&[
0xFE, 0x49, 0x64, 0xB4, 0x59, 0xCF, 0x0C, 0xB2, 0x01, 0xB6, 0x9B, 0x4B, 0xA6, 0x30,
0xF3, 0x4E,
],
);
write_elem(&enc, &mut writer, 0x0010, 0x0032, VR::TM, b"123456");
write_elem(&enc, &mut writer, 0x0008, 0x0119, VR::UC, b"Code");
write_elem(
&enc,
&mut writer,
0x0018,
0x6016,
VR::UL,
&[0x00, 0x00, 0x00, 0x01],
);
write_elem(
&enc,
&mut writer,
0xC001,
0x1234,
VR::UN,
&[0x1, 0x2, 0x3, 0x4, 0x5, 0x6],
);
write_elem(
&enc,
&mut writer,
0x0008,
0x010E,
VR::UR,
b"http://example.com",
);
write_elem(&enc, &mut writer, 0x0008, 0x0040, VR::US, &[0x87, 0x07]);
write_elem(&enc, &mut writer, 0x0018, 0x9917, VR::UT, b"No text ");
write_elem(
&enc,
&mut writer,
0x0008,
0x040C,
VR::UV,
&[0xAB, 0x54, 0xA9, 0x8C, 0xEB, 0x1F, 0x0A, 0xD2],
);
assert_eq!(&buf[..], &RAW[..]);
}
// manually crafting some DICOM sequence/item delimiters
// Tag: (0008,103F) Series Description Code Sequence
// VR: SQ
// Reserved bytes: 0x0000
// Length: 0xFFFF_FFFF
// --
// Tag: (FFFE,E000) Item
// Length: 0xFFFF_FFFF (unspecified)
// --
// Tag: (FFFE,E00D) Item Delimitation Item
// Length: 0
// --
// Tag: (FFFE,E0DD) Sequence Delimitation Item
// Length: 0
// --
const RAW_SEQUENCE_ITEMS: &[u8] = &[
0x00, 0x08, 0x10, 0x3F, b'S', b'Q', 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xE0,
0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xE0, 0x0D, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFE,
0xE0, 0xDD, 0x00, 0x00, 0x00, 0x00,
];
#[test]
fn encode_items() -> Result {
let enc = ExplicitVRBigEndianEncoder::default();
let mut out = Vec::new();
{
let bytes_written = enc.encode_element_header(
&mut out,
DataElementHeader::new(Tag(0x0008, 0x103F), VR::SQ, Length::UNDEFINED),
)?;
assert_eq!(bytes_written, 12);
}
assert_eq!(out.len(), 12);
enc.encode_item_header(&mut out, Length::UNDEFINED.0)?;
assert_eq!(out.len(), 20);
enc.encode_item_delimiter(&mut out)?;
assert_eq!(out.len(), 28);
enc.encode_sequence_delimiter(&mut out)?;
assert_eq!(&out[..], RAW_SEQUENCE_ITEMS);
Ok(())
}
}