Skip to content

Commit fe78b57

Browse files
authored
feat(until) Added untilTag and includeUntilTagValue as an option for DICOM file reading (#195)
* added untilTag as an option for user * added test: 'test_untilTag.js' and setting includeUntilTagValue to false returns vr and values = 0 at tag * quick fix dcmjs path * integrated test_untilTag into npm testing
1 parent 9023d1e commit fe78b57

File tree

3 files changed

+114
-9
lines changed

3 files changed

+114
-9
lines changed

src/DicomMessage.js

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,32 @@ const encapsulatedSyntaxes = [
3030
];
3131

3232
class DicomMessage {
33-
static read(bufferStream, syntax, ignoreErrors) {
33+
static read(
34+
bufferStream,
35+
syntax,
36+
ignoreErrors,
37+
untilTag = null,
38+
includeUntilTagValue = false
39+
) {
3440
var dict = {};
3541
try {
3642
while (!bufferStream.end()) {
37-
var readInfo = DicomMessage.readTag(bufferStream, syntax);
38-
39-
dict[readInfo.tag.toCleanString()] = {
43+
const readInfo = DicomMessage.readTag(
44+
bufferStream,
45+
syntax,
46+
untilTag,
47+
includeUntilTagValue
48+
);
49+
const cleanTagString = readInfo.tag.toCleanString();
50+
51+
dict[cleanTagString] = {
4052
vr: readInfo.vr.type,
4153
Value: readInfo.values
4254
};
55+
56+
if (untilTag && untilTag === cleanTagString) {
57+
break;
58+
}
4359
}
4460
return dict;
4561
} catch (err) {
@@ -67,8 +83,15 @@ class DicomMessage {
6783
return encapsulatedSyntaxes.indexOf(syntax) != -1;
6884
}
6985

70-
static readFile(buffer, options = { ignoreErrors: false }) {
71-
const { ignoreErrors } = options;
86+
static readFile(
87+
buffer,
88+
options = {
89+
ignoreErrors: false,
90+
untilTag: null,
91+
includeUntilTagValue: false
92+
}
93+
) {
94+
const { ignoreErrors, untilTag, includeUntilTagValue } = options;
7295
var stream = new ReadBufferStream(buffer),
7396
useSyntax = EXPLICIT_LITTLE_ENDIAN;
7497
stream.reset();
@@ -86,7 +109,13 @@ class DicomMessage {
86109
//get the syntax
87110
var mainSyntax = metaHeader["00020010"].Value[0];
88111
mainSyntax = DicomMessage._normalizeSyntax(mainSyntax);
89-
var objects = DicomMessage.read(stream, mainSyntax, ignoreErrors);
112+
var objects = DicomMessage.read(
113+
stream,
114+
mainSyntax,
115+
ignoreErrors,
116+
untilTag,
117+
includeUntilTagValue
118+
);
90119

91120
var dicomDict = new DicomDict(metaHeader);
92121
dicomDict.dict = objects;
@@ -122,7 +151,12 @@ class DicomMessage {
122151
return written;
123152
}
124153

125-
static readTag(stream, syntax) {
154+
static readTag(
155+
stream,
156+
syntax,
157+
untilTag = null,
158+
includeUntilTagValue = false
159+
) {
126160
var implicit = syntax == IMPLICIT_LITTLE_ENDIAN ? true : false,
127161
isLittleEndian =
128162
syntax == IMPLICIT_LITTLE_ENDIAN ||
@@ -136,6 +170,12 @@ class DicomMessage {
136170
element = stream.readUint16(),
137171
tag = tagFromNumbers(group, element);
138172

173+
if (untilTag === tag.toCleanString() && untilTag !== null) {
174+
if (!includeUntilTagValue) {
175+
return { tag: tag, vr: 0, values: 0 };
176+
}
177+
}
178+
139179
var length = null,
140180
vr = null,
141181
vrType;

test/test_data.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,35 @@ const tests = {
359359
function writeToBuffer(dicomDict, options) {
360360
return dicomDict.write(options);
361361
}
362-
}
362+
},
363+
test_untiltag: () => {
364+
const buffer = fs.readFileSync(
365+
path.join(__dirname, 'sample-dicom.dcm')
366+
);
367+
console.time('readFile');
368+
const fullData = DicomMessage.readFile(buffer.buffer)
369+
console.timeEnd('readFile');
370+
371+
console.time('readFile without untilTag');
372+
const dicomData = DicomMessage.readFile(buffer.buffer, options={ untilTag: '7FE00010', includeUntilTagValue: false });
373+
console.timeEnd('readFile without untilTag');
374+
375+
console.time('readFile with untilTag');
376+
const dicomData2 = DicomMessage.readFile(buffer.buffer, options={ untilTag: '7FE00010', includeUntilTagValue: true });
377+
console.timeEnd('readFile with untilTag');
378+
379+
const full_dataset = DicomMetaDictionary.naturalizeDataset(fullData.dict);
380+
full_dataset._meta = DicomMetaDictionary.namifyDataset(fullData.meta);
381+
382+
const dataset = DicomMetaDictionary.naturalizeDataset(dicomData.dict);
383+
dataset._meta = DicomMetaDictionary.namifyDataset(dicomData.meta);
384+
385+
const dataset2 = DicomMetaDictionary.naturalizeDataset(dicomData2.dict);
386+
dataset2._meta = DicomMetaDictionary.namifyDataset(dicomData2.meta);
387+
388+
expect(full_dataset.PixelData).to.deep.equal(dataset2.PixelData);
389+
expect(dataset.PixelData).to.deep.equal(0);
390+
},
363391
};
364392

365393
exports.test = async testToRun => {

test/test_untilTag.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const expect = require("chai").expect;
2+
const fs = require('fs');
3+
const path = require("path")
4+
const dcmjs = require('../build/dcmjs.js')
5+
const buffer = fs.readFileSync('sample-dicom.dcm');
6+
7+
const { DicomMessage, DicomMetaDictionary } = dcmjs.data;
8+
9+
console.time('readFile');
10+
const fullData = DicomMessage.readFile(buffer.buffer)
11+
console.timeEnd('readFile');
12+
13+
console.time('readFile without untilTag');
14+
const dicomData = DicomMessage.readFile(buffer.buffer, options={ untilTag: '7FE00010', includeUntilTagValue: false });
15+
console.timeEnd('readFile without untilTag');
16+
17+
console.time('readFile with untilTag');
18+
const dicomData2 = DicomMessage.readFile(buffer.buffer, options={ untilTag: '7FE00010', includeUntilTagValue: true });
19+
console.timeEnd('readFile with untilTag');
20+
21+
const full_dataset = DicomMetaDictionary.naturalizeDataset(fullData.dict);
22+
full_dataset._meta = DicomMetaDictionary.namifyDataset(fullData.meta);
23+
24+
// console.log(full_dataset.PixelData);
25+
26+
const dataset = DicomMetaDictionary.naturalizeDataset(dicomData.dict);
27+
dataset._meta = DicomMetaDictionary.namifyDataset(dicomData.meta);
28+
29+
// console.log(dataset.PixelData);
30+
31+
const dataset2 = DicomMetaDictionary.naturalizeDataset(dicomData2.dict);
32+
dataset2._meta = DicomMetaDictionary.namifyDataset(dicomData2.meta);
33+
34+
// console.log(dataset2.PixelData);
35+
36+
expect(full_dataset.PixelData).to.deep.equal(dataset2.PixelData);
37+
expect(dataset.PixelData).to.deep.equal(0);

0 commit comments

Comments
 (0)