-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathreadDicomElementExplicit_test.js
244 lines (192 loc) · 6.6 KB
/
readDicomElementExplicit_test.js
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
import { expect } from 'chai';
import ByteStream from '../src/byteStream';
import readDicomElementExplicit from '../src/readDicomElementExplicit';
import littleEndianByteArrayParser from '../src/littleEndianByteArrayParser';
function testFourByteLength(vr) {
// Arrange
const byteArray = new Uint8Array(16909060 + 12);
byteArray[0] = 0x11;
byteArray[1] = 0x22;
byteArray[2] = 0x33;
byteArray[3] = 0x44;
byteArray[4] = vr.charCodeAt(0);
byteArray[5] = vr.charCodeAt(1);
byteArray[6] = 0x00;
byteArray[7] = 0x00;
byteArray[8] = 0x04; // 4 overall length = 16909060 = (16777216 + 131072 + 768 + 4)
byteArray[9] = 0x03; // 768
byteArray[10] = 0x02; // 131072
byteArray[11] = 0x01; // 16777216
const byteStream = new ByteStream(littleEndianByteArrayParser, byteArray);
// Act
const element = readDicomElementExplicit(byteStream);
// Assert
expect(element.length).to.equal(16909060);
}
describe('readDicomElementExplicit', () => {
it('should return an element', () => {
// Arrange
const byteArray = new Uint8Array(32);
const byteStream = new ByteStream(littleEndianByteArrayParser, byteArray);
// Act
const element = readDicomElementExplicit(byteStream);
// Assert
expect(element).to.be.ok;
});
it('should parse the tag correctly', () => {
// Arrange
const byteArray = new Uint8Array(32);
byteArray[0] = 0x11;
byteArray[1] = 0x22;
byteArray[2] = 0x33;
byteArray[3] = 0x44;
const byteStream = new ByteStream(littleEndianByteArrayParser, byteArray);
// Act
const element = readDicomElementExplicit(byteStream);
// Assert
expect(element.tag).to.equal('x22114433');
});
it('should parse vr correctly', () => {
// Arrange
const byteArray = new Uint8Array(32);
byteArray[0] = 0x11;
byteArray[1] = 0x22;
byteArray[2] = 0x33;
byteArray[3] = 0x44;
byteArray[4] = 0x53; // ST
byteArray[5] = 0x54;
const byteStream = new ByteStream(littleEndianByteArrayParser, byteArray);
// Act
const element = readDicomElementExplicit(byteStream);
// Assert
expect(element.vr).to.equal('ST');
});
it('should parse element for 2 bytes length correctly', () => {
// Arrange
const byteArray = new Uint8Array(1024);
byteArray[0] = 0x11;
byteArray[1] = 0x22;
byteArray[2] = 0x33;
byteArray[3] = 0x44;
byteArray[4] = 0x53; // ST
byteArray[5] = 0x54;
byteArray[6] = 0x01; // length of 513
byteArray[7] = 0x02;
const byteStream = new ByteStream(littleEndianByteArrayParser, byteArray);
// Act
const element = readDicomElementExplicit(byteStream);
// Assert
expect(element.length).to.equal(513);
});
it('should parse element for 4 bytes length correctly (OB)', () => {
testFourByteLength('OB');
});
it('should parse element for 4 bytes length correctly (OD)', () => {
testFourByteLength('OD');
});
it('should parse element for 4 bytes length correctly (OF)', () => {
testFourByteLength('OF');
});
it('should parse element for 4 bytes length correctly (OL)', () => {
testFourByteLength('OL');
});
it('should parse element for 4 bytes length correctly (OW)', () => {
testFourByteLength('OW');
});
it('should parse element for 4 bytes length correctly (SQ)', () => {
// Arrange
const byteArray = new Uint8Array(16909060 + 12);
byteArray[0] = 0x11;
byteArray[1] = 0x22;
byteArray[2] = 0x33;
byteArray[3] = 0x44;
byteArray[4] = 0x53; // SQ
byteArray[5] = 0x51;
byteArray[6] = 0x00;
byteArray[7] = 0x00;
byteArray[8] = 0x08; // Length = 8
byteArray[9] = 0x00;
byteArray[10] = 0x00;
byteArray[11] = 0x00;
byteArray[12] = 0xFE; // Begin item of zero length
byteArray[13] = 0xFF;
byteArray[14] = 0x00;
byteArray[15] = 0xE0;
byteArray[16] = 0x00;
byteArray[17] = 0x00;
byteArray[18] = 0x00;
byteArray[19] = 0x00;
const byteStream = new ByteStream(littleEndianByteArrayParser, byteArray);
// Act
const element = readDicomElementExplicit(byteStream);
// Assert
expect(element.length).to.equal(8);
});
it('should parse element for 4 bytes length correctly (UC)', () => {
testFourByteLength('UC');
});
it('should parse element for 4 bytes length correctly (UR)', () => {
testFourByteLength('UR');
});
it('should parse element for 4 bytes length correctly (UT)', () => {
testFourByteLength('UT');
});
it('should parse element for 4 bytes length correctly (UN)', () => {
testFourByteLength('UN');
});
it('should parse element and return the right data offset', () => {
// Arrange
const byteArray = new Uint8Array(16909060 + 12);
byteArray[0] = 0x11;
byteArray[1] = 0x22;
byteArray[2] = 0x33;
byteArray[3] = 0x44;
byteArray[4] = 0x4F; // OB
byteArray[5] = 0x42;
byteArray[6] = 0x00;
byteArray[7] = 0x00;
byteArray[8] = 0x04; // 4 overall length = 16909060 = (16777216 + 131072 + 768 + 4)
byteArray[9] = 0x03; // 768
byteArray[10] = 0x02; // 131072
byteArray[11] = 0x01; // 16777216
const byteStream = new ByteStream(littleEndianByteArrayParser, byteArray);
// Act
const element = readDicomElementExplicit(byteStream);
// Assert
expect(element.dataOffset).to.equal(12);
});
it('should parse UN element of implicit length containing an embedded sequence', () => {
// Arrange
const byteArray = new Uint8Array([
0x01, 0x10, 0x00, 0x20, // (1001,2000)
0x55, 0x4E, // UN
0x00, 0x00, // Reserved bytes
0xFF, 0xFF, 0xFF, 0xFF, // Undefined length
// Empty item
0xFE, 0xFF, 0x00, 0xE0,
0xFF, 0xFF, 0xFF, 0xFF,
// Nested empty sequence
0x01, 0x10, 0x02, 0x20, // (1001,2002)
0x53, 0x51, // SQ
0x00, 0x00, // Reserved bytes
0xFF, 0xFF, 0xFF, 0xFF, // Undefined length
0xFE, 0xFF, 0xDD, 0xE0, // End of sequence
0x00, 0x00, 0x00, 0x00,
0xFE, 0xFF, 0x0D, 0xE0, // End of item
0x00, 0x00, 0x00, 0x00,
0xFE, 0xFF, 0xDD, 0xE0, // End of sequence
0x00, 0x00, 0x00, 0x00,
]);
const byteStream = new ByteStream(littleEndianByteArrayParser, byteArray);
// Act
const element = readDicomElementExplicit(byteStream);
// Assert
expect(element.dataOffset).to.equal(12);
expect(element.tag).to.equal('x10012000');
expect(element.vr).to.equal('UN');
expect(element.length).to.equal(44);
expect(element.items.length).to.equal(1);
expect(element.items[0].tag).to.equal('xfffee000');
expect(element.items[0].dataSet.elements).to.have.all.keys(['x10012002']);
});
});