Skip to main content

dcmnorm_encoding/decode/
explicit_le.rs

1//! Explicit VR Little Endian syntax transfer implementation
2
3use crate::decode::basic::LittleEndianBasicDecoder;
4use crate::decode::{
5    BadSequenceHeaderSnafu, BasicDecode, Decode, DecodeFrom, ReadHeaderTagSnafu,
6    ReadItemHeaderSnafu, ReadItemLengthSnafu, ReadLengthSnafu, ReadReservedSnafu, ReadTagSnafu,
7    ReadVrSnafu, Result,
8};
9use byteordered::byteorder::{ByteOrder, LittleEndian};
10use dcmnorm_core::header::{DataElementHeader, Length, SequenceItemHeader};
11use dcmnorm_core::{Tag, VR};
12use snafu::ResultExt;
13use std::io::Read;
14
15/// A data element decoder for the Explicit VR Little Endian transfer syntax.
16#[derive(Debug, Default, Clone)]
17pub struct ExplicitVRLittleEndianDecoder {
18    basic: LittleEndianBasicDecoder,
19}
20
21impl Decode for ExplicitVRLittleEndianDecoder {
22    fn decode_header<S>(&self, mut source: &mut S) -> Result<(DataElementHeader, usize)>
23    where
24        S: ?Sized + Read,
25    {
26        // retrieve tag
27        let Tag(group, element) = self
28            .basic
29            .decode_tag(&mut source)
30            .context(ReadHeaderTagSnafu)?;
31
32        let mut buf = [0u8; 4];
33        if group == 0xFFFE {
34            // item delimiters do not have VR or reserved field
35            source.read_exact(&mut buf).context(ReadItemLengthSnafu)?;
36            let len = LittleEndian::read_u32(&buf);
37            return Ok((
38                DataElementHeader::new((group, element), VR::UN, Length(len)),
39                8, // tag + len
40            ));
41        }
42
43        // retrieve explicit VR
44        source.read_exact(&mut buf[0..2]).context(ReadVrSnafu)?;
45        let vr = VR::from_binary([buf[0], buf[1]]).unwrap_or(VR::UN);
46        let bytes_read;
47
48        // retrieve data length
49        let len = match vr {
50            // PS3.5 7.1.2:
51            // for VRs of AE, AS, AT, CS, DA, DS, DT, FL, FD, IS, LO, LT, PN,
52            // SH, SL, SS, ST, TM, UI, UL and US the Value Length Field is the
53            // 16-bit unsigned integer following the two byte VR Field (Table
54            // 7.1-2). The value of the Value Length Field shall equal the
55            // length of the Value Field.
56            VR::AE
57            | VR::AS
58            | VR::AT
59            | VR::CS
60            | VR::DA
61            | VR::DS
62            | VR::DT
63            | VR::FL
64            | VR::FD
65            | VR::IS
66            | VR::LO
67            | VR::LT
68            | VR::PN
69            | VR::SH
70            | VR::SL
71            | VR::SS
72            | VR::ST
73            | VR::TM
74            | VR::UI
75            | VR::UL
76            | VR::US => {
77                // read 2 bytes for the data length
78                source.read_exact(&mut buf[0..2]).context(ReadLengthSnafu)?;
79                bytes_read = 8;
80                u32::from(LittleEndian::read_u16(&buf[0..2]))
81            }
82            // PS3.5 7.1.2:
83            // for all other VRs the 16 bits following the two byte VR Field
84            // are reserved for use by later versions of the DICOM Standard.
85            // These reserved bytes shall be set to 0000H and shall not be
86            // used or decoded (Table 7.1-1). The Value Length Field is a
87            // 32-bit unsigned integer.
88            _ => {
89                // read 2 reserved bytes, then 4 bytes for data length
90                source
91                    .read_exact(&mut buf[0..2])
92                    .context(ReadReservedSnafu)?;
93                source.read_exact(&mut buf).context(ReadLengthSnafu)?;
94                bytes_read = 12;
95                LittleEndian::read_u32(&buf)
96            }
97        };
98
99        Ok((
100            DataElementHeader::new((group, element), vr, Length(len)),
101            bytes_read,
102        ))
103    }
104
105    fn decode_item_header<S>(&self, source: &mut S) -> Result<SequenceItemHeader>
106    where
107        S: ?Sized + Read,
108    {
109        let mut buf = [0u8; 8];
110        source.read_exact(&mut buf).context(ReadItemHeaderSnafu)?;
111        // retrieve tag
112        let group = LittleEndian::read_u16(&buf[0..2]);
113        let element = LittleEndian::read_u16(&buf[2..4]);
114        let len = LittleEndian::read_u32(&buf[4..8]);
115
116        SequenceItemHeader::new((group, element), Length(len)).context(BadSequenceHeaderSnafu)
117    }
118
119    fn decode_tag<S>(&self, source: &mut S) -> Result<Tag>
120    where
121        S: ?Sized + Read,
122    {
123        let mut buf = [0u8; 4];
124        source.read_exact(&mut buf).context(ReadTagSnafu)?;
125        Ok(Tag(
126            LittleEndian::read_u16(&buf[0..2]),
127            LittleEndian::read_u16(&buf[2..4]),
128        ))
129    }
130}
131
132impl<S: ?Sized> DecodeFrom<S> for ExplicitVRLittleEndianDecoder
133where
134    S: Read,
135{
136    #[inline]
137    fn decode_header(&self, source: &mut S) -> Result<(DataElementHeader, usize)> {
138        Decode::decode_header(self, source)
139    }
140
141    #[inline]
142    fn decode_item_header(&self, source: &mut S) -> Result<SequenceItemHeader> {
143        Decode::decode_item_header(self, source)
144    }
145
146    #[inline]
147    fn decode_tag(&self, source: &mut S) -> Result<Tag> {
148        Decode::decode_tag(self, source)
149    }
150
151    #[inline]
152    fn is_explicit_vr(&self) -> bool {
153        true
154    }
155}
156
157#[cfg(test)]
158mod tests {
159    use super::ExplicitVRLittleEndianDecoder;
160    use crate::decode::Decode;
161    use dcmnorm_core::header::{HasLength, Header, Length};
162    use dcmnorm_core::{Tag, VR};
163    use std::io::{Cursor, Seek, SeekFrom};
164
165    // manually crafting some DICOM data elements
166    #[rustfmt::skip]
167    const RAW: &[u8] = &[
168        0x02, 0x00, 0x02, 0x00,     // (0002,0002) (LE) Media Storage SOP Class UID
169            b'U', b'I',             // VR: UI (UID)
170            0x1A, 0x00,             // Length: 26 bytes (LE)
171                // UID: 1.2.840.10008.5.1.4.1.1.1
172                b'1', b'.', b'2', b'.', b'8', b'4', b'0', b'.', b'1', b'0', b'0', b'0', b'8', b'.',
173                b'5', b'.', b'1', b'.', b'4', b'.', b'1', b'.', b'1', b'.', b'1',
174                0x00,               // Padding to make length even
175        0x02, 0x00, 0x10, 0x00,     // (0002,0010) (LE) Transfer Syntax UID
176            b'U', b'I',             // VR: UI (UID)
177            0x14, 0x00,             // Length: 20 bytes (LE)
178                // UID: 1.2.840.10008.1.2.1 (ExplicitVRLittleEndian)
179                b'1', b'.', b'2', b'.', b'8', b'4', b'0', b'.', b'1', b'0', b'0', b'0', b'8', b'.',
180                b'1', b'.', b'2', b'.', b'1',
181                0x00,               // Padding to make length even
182        0x08, 0x00, 0x54, 0x00,     // (0008,0054) (LE) Retrieve AE Title
183            b'A', b'E',             // VR: AE (Application Entity)
184            0x06, 0x00,             // Length: 6 bytes (LE)
185                // String "TITLE"
186                b'T', b'I', b'T', b'L', b'E',
187                b' ',               // Padding to make length even
188        0x10, 0x00, 0x10, 0x10,     // (0010,1010) (LE) Patient's Age
189            b'A', b'S',             // VR: AS (Age String)
190            0x02, 0x00,             // Length: 2 bytes (LE)
191                // String "8Y"
192                b'8', b'Y',
193        0x72, 0x00, 0x26, 0x00,     // (0072,0026) (LE) Selector Attribute
194            b'A', b'T',             // VR: AT (Attribute Tag)
195            0x04, 0x00,             // Length: 4 bytes (LE)
196                // Tag: (0028,2110) (LE) Lossy Image Compression
197                0x28, 0x00, 0x10, 0x21,
198        0x10, 0x00, 0x40, 0x00,     // (0010,0040) (LE) Patient Sex
199            b'C', b'S',             // VR: CS (Code String)
200            0x02, 0x00,             // Length: 2 bytes (LE)
201                // Text: "O"
202                b'O',
203                b' ',               // Padding to make length even
204        0x10, 0x00, 0x30, 0x00,     // (0010,0030) (LE) Patient's Birth Date
205            b'D', b'A',             // VR: DA (Date)
206            0x08, 0x00,             // Length: 8 bytes (LE)
207                // String "19800101"
208                b'1', b'9', b'8', b'0', b'0', b'1', b'0', b'1',
209        0x10, 0x00, 0x20, 0x10,     // (0010,1020) (LE) Patient's size
210            b'D', b'S',             // VR: DS (Decimal String)
211            0x04, 0x00,             // Length: 4 bytes (LE)
212                // String: "1.70"
213                b'1', b'.', b'7', b'0',
214        0x08, 0x00, 0x15, 0x00,     // (0008,0015) (LE) Instance coercion datetime
215            b'D', b'T',             // VR: DT (Date Time)
216            0x0E, 0x00,             // Length: 14 bytes (LE)
217                // String "20051231235960"
218                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',
219        0x10, 0x00, 0x31, 0x94,     // (0010,9431) (LE) Examined Body Thickness
220            b'F', b'L',             // VR: FL (Floating-point IEEE-754)
221            0x04, 0x00,             // Length: 4 bytes (LE)
222                // Value: 3.1415927 (LE)
223                0xDB, 0x0F, 0x49, 0x40,
224        0x40, 0x00, 0x25, 0x92,     // (0040,9225) (LE) Real World Value Slope
225            b'F', b'D',             // VR: FD (Double-precision floating-point IEEE-754)
226            0x08, 0x00,             // Length: 8 bytes (LE)
227                // Value: 3.141592653589793 (LE)
228                0x18, 0x2D, 0x44, 0x54, 0xFB, 0x21, 0x09, 0x40,
229        0x20, 0x00, 0x13, 0x00,     // (0020,0013) (LE) Instance Number
230            b'I', b'S',             // VR: IS (Integer String)
231            0x08, 0x00,             // Length: 8 bytes (LE)
232                // String: "1234567"
233                b'1', b'2', b'3', b'4', b'5', b'6', b'7',
234                b' ',               // Padding to make length even
235        0x10, 0x00, 0x20, 0x00,     // (0010,0020) (LE) Patient ID
236            b'L', b'O',             // VR: LO (Long string)
237            0x0A, 0x00,             // Length: 10 bytes (LE)
238                // String "P12345678X"
239                b'P', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'X',
240        0x10, 0x00, 0x00, 0x40,     // (0010,4000) (LE) Patient Comments
241            b'L', b'T',             // VR: LT (Long Text)
242            0x04, 0x00,             // Length: 4 bytes (LE)
243                // String "None"
244                b'N', b'o', b'n', b'e',
245        0x08, 0x00, 0x1B, 0x04,     // (0008,041B) (LE) RecordKey
246            b'O', b'B',             // VR: OB (Other Byte)
247            0x00, 0x00,             // Reserved, always 0
248            0x02, 0x00, 0x00, 0x00, // Length: 2 bytes (LE)
249                // Value: [0x12, 0x34]
250                0x12, 0x34,
251        0xE0, 0x7F, 0x09, 0x00,     // (7FE0,0009) (LE) Double Float Pixel Data
252            b'O', b'D',             // VR: OD (Other Double)
253            0x00, 0x00,             // Reserved, always 0
254            0x08, 0x00, 0x00, 0x00, // Length: 8 bytes (LE)
255                // Value: [3.141592653589793] (LE)
256                0x18, 0x2D, 0x44, 0x54, 0xFB, 0x21, 0x09, 0x40,
257        0xE0, 0x7F, 0x08, 0x00,     // (7FE0,0008) (LE) Float Pixel Data
258            b'O', b'F',             // VR: OF (Other Float)
259            0x00, 0x00,             // Reserved, always 0
260            0x04, 0x00, 0x00, 0x00, // Length: 4 bytes (LE)
261                // Value: [3.1415927] (LE)
262                0xDB, 0x0F, 0x49, 0x40,
263        0x72, 0x00, 0x75, 0x00,     // (0072,0075) (LE) Selector OL Value
264            b'O', b'L',             // VR: OL (Other Long)
265            0x00, 0x00,             // Reserved, always 0
266            0x04, 0x00, 0x00, 0x00, // Length: 4 bytes (LE)
267                // Value: [0x12345678] (LE)
268                0x78, 0x56, 0x34, 0x12,
269        0x72, 0x00, 0x81, 0x00,     // (0072,0081) (LE) Selector OV Value
270            b'O', b'V',             // VR: OV (Other Very long)
271            0x00, 0x00,             // Reserved, always 0
272            0x08, 0x00, 0x00, 0x00, // Length: 8 bytes (LE)
273                // Value: [0x192A3B4C5D6E7F80] (LE)
274                0x80, 0x7F, 0x6E, 0x5D, 0x4C, 0x3B, 0x2A, 0x19,
275        0x72, 0x00, 0x69, 0x00,     // (0072,0069) (LE) Selector OW Value
276            b'O', b'W',             // VR: OW (Other Word)
277            0x00, 0x00,             // Reserved, always 0
278            0x02, 0x00, 0x00, 0x00, // Length: 2 bytes (LE)
279                // Value: [0x1234] (LE)
280                0x34, 0x12,
281        0x10, 0x00, 0x10, 0x00,     // (0010,0010) (LE) Patient Name
282            b'P', b'N',             // VR: PN (Person Name)
283            0x08, 0x00,             // Length: 8 bytes (LE)
284                // String: "Doe^John"
285                b'D', b'o', b'e', b'^', b'J', b'o', b'h', b'n',
286        0x40, 0x00, 0x10, 0x92,     // (0040,9210) (LE) LUT Label
287            b'S', b'H',             // VR: SH (Short string)
288            0x04, 0x00,             // Length: 4 bytes (LE)
289                // String: "LBL"
290                b'L', b'B', b'L',
291                b' ',               // Padding to make length even
292        0x18, 0x00, 0x20, 0x60,     // (0018,6020) (LE) Reference Pixel X0
293            b'S', b'L',             // VR: SL (Signed Long)
294            0x04, 0x00,             // Length: 4 bytes (LE)
295                // Value: -12345678 (LE)
296                0xB2, 0x9E, 0x43, 0xFF,
297        // Sequences (VR: SQ) tested elsewhere
298        0x28, 0x00, 0x03, 0x95,     // (0028,9503) (LE) Vertices of the Region (VM 2-2n)
299            b'S', b'S',             // VR: SS (Signed Short)
300            0x04, 0x00,             // Length: 4 bytes (2 * 2) (LE)
301                // Value: -4567 (LE)
302                0x29, 0xEE,
303                // Value: 4321 (LE)
304                0xE1, 0x10,
305        0x40, 0x00, 0x80, 0x02,     // (0040,0280) (LE) Comments on the Performed Procedure Step
306            b'S', b'T',             // VR: ST (Short Text)
307            0x0A, 0x00,             // Length: 10 bytes (LE)
308                // String: "No comment"
309                b'N', b'o', b' ', b'c', b'o', b'm', b'm', b'e', b'n', b't',
310        0x72, 0x00, 0x82, 0x00,     // (0072,0082) (LE) SelectorSVValue (VM: 1-n)
311            b'S', b'V',             // VR: SV (Signed Very long)
312            0x00, 0x00,             // Reserved, always 0
313            0x10, 0x00, 0x00, 0x00, // Length: 16 bytes (2 * 8) (LE)
314                // Value: -123456789012345678 (LE)
315                0xB2, 0x0C, 0xCF, 0x59, 0xB4, 0x64, 0x49, 0xFE,
316                // Value: 123456789012345678 (LE)
317                0x4E, 0xF3, 0x30, 0xA6, 0x4B, 0x9B, 0xB6, 0x01,
318        0x10, 0x00, 0x32, 0x00,     // (0010,0032) (LE) Patient's Birth Time
319            b'T', b'M',             // VR: TM (Time)
320            0x06, 0x00,             // Length: 6 bytes (LE)
321                // String: "123456"
322                b'1', b'2', b'3', b'4', b'5', b'6',
323        0x08, 0x00, 0x19, 0x01,     // (0008,0119) (LE) Long Code Value
324            b'U', b'C',             // VR: UC (Unlimited Characters)
325            0x00, 0x00,             // Reserved, always 0
326            0x04, 0x00, 0x00, 0x00, // Length: 4 bytes (LE)
327                // String: "Code"
328                b'C', b'o', b'd', b'e',
329        // UI already tested above
330        0x18, 0x00, 0x16, 0x60,     // (0018,6016) (LE) Region Flags
331            b'U', b'L',
332            0x04, 0x00,             // Length: 4 bytes (LE)
333                // Value: 1 (LE)
334                0x01, 0x00, 0x00, 0x00,
335        0x01, 0xC0, 0x34, 0x12,     // (C001,1234) (LE) (Private data element)
336            b'U', b'N',             // VR: UN (Unknown)
337            0x00, 0x00,             // Reserved, always 0
338            0x06, 0x00, 0x00, 0x00, // Length: 6 bytes (LE)
339                // Value: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06]
340                0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
341        0x08, 0x00, 0x0E, 0x01,     // (0008,010E) (LE) Coding Scheme URL
342            b'U', b'R',             // VR: UR (Universal Resource Locator, URL)
343            0x00, 0x00,             // Reserved, always 0
344            0x12, 0x00, 0x00, 0x00, // Length: 18 bytes (LE)
345                // String: "http://example.com"
346                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',
347                b'.', b'c', b'o', b'm',
348        0x08, 0x00, 0x40, 0x00,     // (0008,0040) (LE) Data Set Type
349            b'U', b'S',             // VR: US (Unsigned Short)
350            0x02, 0x00,             // Length: 2 bytes (LE)
351                // Value: 34567 (LE)
352                0x07, 0x87,
353        0x18, 0x00, 0x17, 0x99,     // (0018,9917) (LE) Instruction Description
354            b'U', b'T',             // VR: UT (Unlimited Text)
355            0x00, 0x00,             // Reserved, always 0
356            0x08, 0x00, 0x00, 0x00, // Length: 8 bytes (LE)
357                // String: "No text"
358                b'N', b'o', b' ', b't', b'e', b'x', b't',
359                b' ',               // Padding to make length even
360        0x08, 0x00, 0x0C, 0x04,     // (0008,040C) (LE) File Offset in Container
361            b'U', b'V',             // VR: UV (Unsigned Very long)
362            0x00, 0x00,             // Reserved, always 0
363            0x08, 0x00, 0x00, 0x00, // Length: 8 bytes (LE)
364                // Value: 12345678901234567890 (LE)
365                0xD2, 0x0A, 0x1F, 0xEB, 0x8C, 0xA9, 0x54, 0xAB,
366    ];
367
368    #[test]
369    fn decode_data_elements() {
370        let dec = ExplicitVRLittleEndianDecoder::default();
371        let mut cursor = Cursor::new(RAW);
372
373        fn read_n<'a>(cursor: &mut Cursor<&'a [u8]>, n: usize) -> &'a [u8] {
374            let pos = cursor.position() as usize;
375            let slice = &cursor.get_ref()[pos..pos + n]; // panic if too short
376            cursor.set_position((pos + n) as u64);
377            slice
378        }
379
380        fn test_vr(
381            dec: &ExplicitVRLittleEndianDecoder,
382            cursor: &mut Cursor<&[u8]>,
383            group: u16,
384            element: u16,
385            vr: VR,
386            value: &[u8],
387        ) {
388            let (elem, _bytes_read) = dec.decode_header(cursor).expect("should find an element");
389            assert_eq!(elem.tag(), Tag(group, element));
390            assert_eq!(elem.vr(), vr);
391            assert_eq!(elem.length(), Length(value.len() as u32));
392            let buffer = read_n(cursor, value.len());
393            assert_eq!(&buffer, &value);
394        }
395
396        // read first element
397        let (elem, bytes_read) = dec
398            .decode_header(&mut cursor)
399            .expect("should find an element");
400        assert_eq!(elem.tag(), Tag(0x0002, 0x0002));
401        assert_eq!(elem.vr(), VR::UI);
402        assert_eq!(elem.length(), Length(26));
403        assert_eq!(bytes_read, 8);
404        // read only half of the value data
405        let buffer = read_n(&mut cursor, 13);
406        assert_eq!(&buffer, b"1.2.840.10008");
407
408        // cursor should now be @ #21 (there is no automatic skipping)
409        assert_eq!(cursor.stream_position().unwrap(), 21);
410        // cursor should now be @ #34 after skipping
411        assert_eq!(cursor.seek(SeekFrom::Current(13)).unwrap(), 34);
412
413        // read second element
414        let (elem, _bytes_read) = dec
415            .decode_header(&mut cursor)
416            .expect("should find an element");
417        assert_eq!(elem.tag(), Tag(0x0002, 0x0010));
418        assert_eq!(elem.vr(), VR::UI);
419        assert_eq!(elem.length(), Length(20));
420        // read all data
421        let buffer = read_n(&mut cursor, 20);
422        assert_eq!(&buffer, b"1.2.840.10008.1.2.1\0");
423
424        // read various VRs
425        test_vr(&dec, &mut cursor, 0x0008, 0x0054, VR::AE, b"TITLE ");
426        test_vr(&dec, &mut cursor, 0x0010, 0x1010, VR::AS, b"8Y");
427        test_vr(
428            &dec,
429            &mut cursor,
430            0x0072,
431            0x0026,
432            VR::AT,
433            &[0x28, 0x00, 0x10, 0x21],
434        );
435        test_vr(&dec, &mut cursor, 0x0010, 0x0040, VR::CS, b"O ");
436        test_vr(&dec, &mut cursor, 0x0010, 0x0030, VR::DA, b"19800101");
437        test_vr(&dec, &mut cursor, 0x0010, 0x1020, VR::DS, b"1.70");
438        test_vr(&dec, &mut cursor, 0x0008, 0x0015, VR::DT, b"20051231235960");
439        test_vr(
440            &dec,
441            &mut cursor,
442            0x0010,
443            0x9431,
444            VR::FL,
445            &[0xDB, 0x0F, 0x49, 0x40],
446        );
447        test_vr(
448            &dec,
449            &mut cursor,
450            0x0040,
451            0x9225,
452            VR::FD,
453            &[0x18, 0x2D, 0x44, 0x54, 0xFB, 0x21, 0x09, 0x40],
454        );
455        test_vr(&dec, &mut cursor, 0x0020, 0x0013, VR::IS, b"1234567 ");
456        test_vr(&dec, &mut cursor, 0x0010, 0x0020, VR::LO, b"P12345678X");
457        test_vr(&dec, &mut cursor, 0x0010, 0x4000, VR::LT, b"None");
458        test_vr(&dec, &mut cursor, 0x0008, 0x041B, VR::OB, &[0x12, 0x34]);
459        test_vr(
460            &dec,
461            &mut cursor,
462            0x7FE0,
463            0x0009,
464            VR::OD,
465            &[0x18, 0x2D, 0x44, 0x54, 0xFB, 0x21, 0x09, 0x40],
466        );
467        test_vr(
468            &dec,
469            &mut cursor,
470            0x7FE0,
471            0x0008,
472            VR::OF,
473            &[0xDB, 0x0F, 0x49, 0x40],
474        );
475        test_vr(
476            &dec,
477            &mut cursor,
478            0x0072,
479            0x0075,
480            VR::OL,
481            &[0x78, 0x56, 0x34, 0x12],
482        );
483        test_vr(
484            &dec,
485            &mut cursor,
486            0x0072,
487            0x0081,
488            VR::OV,
489            &[0x80, 0x7F, 0x6E, 0x5D, 0x4C, 0x3B, 0x2A, 0x19],
490        );
491        test_vr(&dec, &mut cursor, 0x0072, 0x0069, VR::OW, &[0x34, 0x12]);
492        test_vr(&dec, &mut cursor, 0x0010, 0x0010, VR::PN, b"Doe^John");
493        test_vr(&dec, &mut cursor, 0x0040, 0x9210, VR::SH, b"LBL ");
494        test_vr(
495            &dec,
496            &mut cursor,
497            0x0018,
498            0x6020,
499            VR::SL,
500            &[0xB2, 0x9E, 0x43, 0xFF],
501        );
502        test_vr(
503            &dec,
504            &mut cursor,
505            0x0028,
506            0x9503,
507            VR::SS,
508            &[0x29, 0xEE, 0xE1, 0x10],
509        );
510        test_vr(&dec, &mut cursor, 0x0040, 0x0280, VR::ST, b"No comment");
511        test_vr(
512            &dec,
513            &mut cursor,
514            0x0072,
515            0x0082,
516            VR::SV,
517            &[
518                0xB2, 0x0C, 0xCF, 0x59, 0xB4, 0x64, 0x49, 0xFE, 0x4E, 0xF3, 0x30, 0xA6, 0x4B, 0x9B,
519                0xB6, 0x01,
520            ],
521        );
522        test_vr(&dec, &mut cursor, 0x0010, 0x0032, VR::TM, b"123456");
523        test_vr(&dec, &mut cursor, 0x0008, 0x0119, VR::UC, b"Code");
524        test_vr(
525            &dec,
526            &mut cursor,
527            0x0018,
528            0x6016,
529            VR::UL,
530            &[0x01, 0x00, 0x00, 0x00],
531        );
532        test_vr(
533            &dec,
534            &mut cursor,
535            0xC001,
536            0x1234,
537            VR::UN,
538            &[0x1, 0x2, 0x3, 0x4, 0x5, 0x6],
539        );
540        test_vr(
541            &dec,
542            &mut cursor,
543            0x0008,
544            0x010E,
545            VR::UR,
546            b"http://example.com",
547        );
548        test_vr(&dec, &mut cursor, 0x0008, 0x0040, VR::US, &[0x07, 0x87]);
549        test_vr(&dec, &mut cursor, 0x0018, 0x9917, VR::UT, b"No text ");
550        test_vr(
551            &dec,
552            &mut cursor,
553            0x0008,
554            0x040C,
555            VR::UV,
556            &[0xD2, 0x0A, 0x1F, 0xEB, 0x8C, 0xA9, 0x54, 0xAB],
557        );
558    }
559
560    // manually crafting some DICOM sequence/item delimiters
561    //  Tag: (0008,103F) Series Description Code Sequence
562    //  VR: SQ
563    //  Reserved bytes: 0x0000
564    //  Length: 0xFFFF_FFFF
565    // --
566    //  Tag: (FFFE,E000) Item
567    //  Length: 0xFFFF_FFFF (unspecified)
568    // --
569    //  Tag: (FFFE,E00D) Item Delimitation Item
570    //  Length: 0
571    // --
572    //  Tag: (FFFE,E0DD) Sequence Delimitation Item
573    //  Length: 0
574    // --
575    const RAW_SEQUENCE_ITEMS: &[u8] = &[
576        0x08, 0x00, 0x3F, 0x10, b'S', b'Q', 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0x00,
577        0xE0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0x0D, 0xE0, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFF,
578        0xDD, 0xE0, 0x00, 0x00, 0x00, 0x00,
579    ];
580
581    #[test]
582    fn decode_items() {
583        let dec = ExplicitVRLittleEndianDecoder::default();
584        let mut cursor = Cursor::new(RAW_SEQUENCE_ITEMS);
585        {
586            // read first element
587            let (elem, bytes_read) = dec
588                .decode_header(&mut cursor)
589                .expect("should find an element header");
590            assert_eq!(elem.tag(), Tag(8, 0x103F));
591            assert_eq!(elem.vr(), VR::SQ);
592            assert!(elem.length().is_undefined());
593            assert_eq!(bytes_read, 12);
594        }
595        // cursor should now be @ #12
596        assert_eq!(cursor.stream_position().unwrap(), 12);
597        {
598            let elem = dec
599                .decode_item_header(&mut cursor)
600                .expect("should find an item header");
601            assert!(elem.is_item());
602            assert_eq!(elem.tag(), Tag(0xFFFE, 0xE000));
603            assert!(elem.length().is_undefined());
604        }
605        // cursor should now be @ #20
606        assert_eq!(cursor.stream_position().unwrap(), 20);
607        {
608            let elem = dec
609                .decode_item_header(&mut cursor)
610                .expect("should find an item header");
611            assert!(elem.is_item_delimiter());
612            assert_eq!(elem.tag(), Tag(0xFFFE, 0xE00D));
613            assert_eq!(elem.length(), Length(0));
614        }
615        // cursor should now be @ #28
616        assert_eq!(cursor.stream_position().unwrap(), 28);
617        {
618            let elem = dec
619                .decode_item_header(&mut cursor)
620                .expect("should find an item header");
621            assert!(elem.is_sequence_delimiter());
622            assert_eq!(elem.tag(), Tag(0xFFFE, 0xE0DD));
623            assert_eq!(elem.length(), Length(0));
624        }
625    }
626}