-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdicomWriter.js
359 lines (320 loc) · 10.1 KB
/
dicomWriter.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
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
(function (root, factory) {
// node.js
if (typeof module !== 'undefined' && module.exports) {
module.exports = factory();
}
else if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else {
// Browser globals
if(typeof cornerstone === 'undefined'){
dicomWriter = {};
// meteor
if (typeof Package !== 'undefined') {
root.dicomWriter = dicomWriter;
}
}
dicomWriter = factory();
}
}(this, function () {
/**
* Utility functions for dealing with DICOM
*/
var dicomWriter = (function (dicomWriter)
{
"use strict";
if(dicomWriter === undefined)
{
dicomWriter = {};
}
var stringVrs = {
AE: true,
AS: true,
AT: false,
CS: true,
DA: true,
DS: true,
DT: true,
FL: false,
FD: false,
IS: true,
LO: true,
LT: true,
OB: false,
OD: false,
OF: false,
OW: false,
PN: true,
SH: true,
SL: false,
SQ: false,
SS: false,
ST: true,
TM: true,
UI: true,
UL: false,
UN: undefined, // dunno
UR: true,
US: false,
UT: true
};
/**
* Tests to see if vr is a string or not.
* @param vr
* @returns true if string, false it not string, undefined if unknown vr or UN type
*/
dicomWriter.isStringVr = function(vr)
{
return stringVrs[vr];
};
/**
* Tests to see if a given tag in the format xggggeeee is a private tag or not
* @param tag
* @returns {boolean}
*/
dicomWriter.isPrivateTag = function(tag)
{
var lastGroupDigit = parseInt(tag[4]);
var groupIsOdd = (lastGroupDigit % 2) === 1;
return groupIsOdd;
};
return dicomWriter;
}(dicomWriter));
/**
*
* The DataSet class encapsulates a collection of DICOM Elements and provides various functions
* to access the data in those elements
*
* Rules for handling padded spaces:
* DS = Strip leading and trailing spaces
* DT = Strip trailing spaces
* IS = Strip leading and trailing spaces
* PN = Strip trailing spaces
* TM = Strip trailing spaces
* AE = Strip leading and trailing spaces
* CS = Strip leading and trailing spaces
* SH = Strip leading and trailing spaces
* LO = Strip leading and trailing spaces
* LT = Strip trailing spaces
* ST = Strip trailing spaces
* UT = Strip trailing spaces
*
*/
var dicomWriter = (function (dicomWriter)
{
"use strict";
if(dicomWriter === undefined)
{
dicomWriter = {};
}
function getByteArrayParser(element, defaultParser)
{
return (element.parser !== undefined ? element.parser : defaultParser);
}
function getDataLengthSizeInBytesForVR(vr)
{
if( vr === 'OB' ||
vr === 'OW' ||
vr === 'SQ' ||
vr === 'OF' ||
vr === 'UT' ||
vr === 'UN')
{
return 4;
}
else
{
return 2;
}
}
/**
* Constructs a new DataSet given byteArray and collection of elements
* @param byteArrayParser
* @param byteArray
* @param elements
* @constructor
*/
dicomWriter.DataSet = function(byteArrayParser, byteArray, elements)
{
this.byteArrayParser = byteArrayParser;
this.byteArray = Buffer.from(byteArray)
this.elements = elements;
this.offsetChanges = [];
this.totalOffsetChanges = [];
};
dicomWriter.DataSet.prototype.getOffsetChange = function(element) {
var offsetChange = 0;
var currOffset = element.offset;
this.offsetChanges.forEach(function(offsetChange) {
if (offsetChange.offset < currOffset) {
offsetChange += offsetChange.change;
}
});
return offsetChange;
};
/**
* Returns a string for the element. If index is provided, the element is assumed to be
* multi-valued and will return the component specified by index. Undefined is returned
* if there is no component with the specified index, the element does not exist or is zero length.
*
* Use this function for VR types of AE, CS, SH and LO
*
* @param tag The DICOM tag in the format xGGGGEEEE
* @param index the index of the desired value in a multi valued string or undefined for the entire string
* @returns {*}
*/
dicomWriter.DataSet.prototype.changeString = function(tag, newString)
{
var element = this.elements[tag];
if(element && element.length > 0 && dicomWriter.isStringVr(element.vr))
{
var dataOffset = element.dataOffset + this.getOffsetChange(element);
var result = dicomWriter.writeFixedString(this.byteArray, dataOffset, element.length, newString);
this.byteArray = result.byteArray;
var newLength = result.newLength;
var diff = newLength - element.length;
element.length = newLength;
if (element.parser) { // explicit
var dataLengthSizeBytes = getDataLengthSizeInBytesForVR(element.vr);
if (dataLengthSizeBytes === 2) {
if (element.parser.bigEndian) {
this.byteArray.writeUInt16BE(newLength, element.dataOffset - dataLengthSizeBytes);
} else {
this.byteArray.writeUInt16LE(newLength, element.dataOffset - dataLengthSizeBytes);
}
} else {
if (element.parser.bigEndian) {
this.byteArray.writeUInt32BE(newLength, element.dataOffset - dataLengthSizeBytes);
} else {
this.byteArray.writeUInt32LE(newLength, element.dataOffset - dataLengthSizeBytes);
}
}
} else {
if (this.byteArrayParser.bigEndian) {
this.byteArray.writeUInt32BE(newLength, element.dataOffset - 4);
} else {
this.byteArray.writeUInt32LE(newLength, element.dataOffset - 4);
}
}
this.offsetChanges.push({offset: element.dataOffset, change: diff});
return true;
}
return undefined;
};
dicomWriter.DataSet.prototype.setupTotalOffsets = function() {
var totalOffsetChange = 0;
this.offsetChanges.sort(function(a, b) {
if (a.offset > b.offset) {
return 1;
} else if (a.offset < b.offset) {
return -1;
} else {
return 0;
}
});
this.totalOffsetChanges = this.offsetChanges.map(function(offsetChange) {
totalOffsetChange += offsetChange.change;
return {offset: offsetChange.offset, change: totalOffsetChange};
});
this.totalOffsetChanges.sort(function(a, b) {
if (a.offset > b.offset) {
return -1;
} else if (a.offset < b.offset) {
return 1;
} else {
return 0;
}
});
};
dicomWriter.DataSet.prototype.getTotalOffsetChange = function(element) {
var offsetChange = 0;
var currOffset = element.offset;
this.totalOffsetChanges.find(function(totalOffsetChange) {
return totalOffsetChange.offset > currOffset;
});
return offsetChange;
};
/**
* Returns a string for the element. If index is provided, the element is assumed to be
* multi-valued and will return the component specified by index. Undefined is returned
* if there is no component with the specified index, the element does not exist or is zero length.
*
* Use this function for VR types of AE, CS, SH and LO
*
* @param tag The DICOM tag in the format xGGGGEEEE
* @param index the index of the desired value in a multi valued string or undefined for the entire string
* @returns {*}
*/
dicomWriter.DataSet.prototype.finishedChanges = function()
{
this.setupTotalOffsets();
for (var tag in this.elements) {
if (this.elements.hasOwnProperty(tag)) {
var element = this.elements[tag];
element.dataOffset = element.dataOffset + this.getTotalOffsetChange(element);
}
}
};
//dicomWriter.DataSet = DataSet;
return dicomWriter;
}(dicomWriter));
/**
* Internal helper functions common to parsing byte arrays of any type
*/
var dicomWriter = (function (dicomWriter)
{
"use strict";
if(dicomWriter === undefined)
{
dicomWriter = {};
}
/**
* Reads a string of 8-bit characters from an array of bytes and advances
* the position by length bytes. A null terminator will end the string
* but will not effect advancement of the position. Trailing and leading
* spaces are preserved (not trimmed)
* @param byteArray the byteArray to read from
* @param position the position in the byte array to read from
* @param length the maximum number of bytes to parse
* @returns {string} the parsed string
* @throws error if buffer overread would occur
* @access private
*/
dicomWriter.writeFixedString = function(byteArray, position, length, newString)
{
if(length < 0)
{
throw 'dicomWriter.writeFixedString - length cannot be less than 0';
}
if(position + length > byteArray.length) {
throw 'dicomWriter.writeFixedString: attempt to read past end of buffer';
}
var newLength = 0;
var newBuffer = [];
var stringLength = newString.length;
for(var i = 0; i < stringLength; i++) {
newBuffer.push(newString.charCodeAt(i));
newLength++;
}
var buffer = Buffer.from(newBuffer);
byteArray = Buffer.concat([byteArray.slice(0, position), buffer, byteArray.slice(position + length)]);
return {byteArray: byteArray, newLength: newLength};
};
return dicomWriter;
}(dicomWriter));
/**
* Version
*/
var dicomWriter = (function (dicomWriter)
{
"use strict";
if(dicomWriter === undefined)
{
dicomWriter = {};
}
dicomWriter.version = "1.0.0";
return dicomWriter;
}(dicomWriter));
return dicomWriter;
}));