Skip to content

Commit

Permalink
Merge pull request #215 from yagni/callback-all-tags
Browse files Browse the repository at this point in the history
Use vr callback on all tags, not just to determine sequence.
  • Loading branch information
yagni authored Oct 11, 2022
2 parents 2c9ee43 + 6ed4f2c commit 98e991d
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 19 deletions.
File renamed without changes.
10 changes: 7 additions & 3 deletions examples/dumpWithDataDictionary/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ <h1>DICOM Dump with Data Dictionary v<span id="version"></span></h1>
<script src="../pako.min.js"></script>

<!-- include the data dictionary -->
<script src="dataDictionary.js"></script>
<script src="../dataDictionary.js"></script>

<!-- include the uids -->
<script src="uids.js"></script>
Expand Down Expand Up @@ -626,11 +626,15 @@ <h1>DICOM Dump with Data Dictionary v<span id="version"></span></h1>
setTimeout(function() {
var sha1Hash = sha1(byteArray, 0, byteArray.length);

// Invoke the paresDicom function and get back a DataSet object with the contents
// Invoke the parseDicom function and get back a DataSet object with the contents
try {
var start = new Date().getTime();
var options = {
untilTag: untilTag
untilTag: untilTag,
vrCallback(tag) {
const formatted = `(${tag.substring(1, 5).toUpperCase()},${tag.substring(5, 9).toUpperCase()})`;
return !!TAG_DICT[formatted] ? TAG_DICT[formatted].vr : undefined;
}
};

dataSet = dicomParser.parseDicom(byteArray, options);
Expand Down
12 changes: 9 additions & 3 deletions examples/explicitDataSetToJS/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ <h1>DICOM Dump using explicitDataSetToJS</h1>
</p>
<p>
This example illustrates how to recursively iterate over a parsed data set to dump all data elements
into a tree like structure using the explicitDataSetToJS utility function. Note that DICOM files dropped here are not uploaded anywhere, all processing
into a tree like structure using the explicitDataSetToJS utility function. This example parses using
a data dictionary via the vrCallback option in order to use explicitDataSetToJS even with implicit
VR data sets. Note that DICOM files dropped here are not uploaded anywhere, all processing
is done inside your web browser in Javascript.
</p>
<strong>Use of this example require IE10+ or any other modern browser.</strong>
Expand Down Expand Up @@ -59,6 +61,7 @@ <h1>DICOM Dump using explicitDataSetToJS</h1>

<!-- include the dicomParser library -->
<script src="../../dist/dicomParser.js"></script>
<script src="../dataDictionary.js"></script>
<script>window.dicomParser || document.write('<script src="https://unpkg.com/dicom-parser">\x3C/script>')</script>

<!-- jquery - included to make things easier to demo, not needed by dicomParser -->
Expand Down Expand Up @@ -138,11 +141,14 @@ <h1>DICOM Dump using explicitDataSetToJS</h1>
// set a short timeout to do the parse so the DOM has time to update itself with the above message
setTimeout(function() {

// Invoke the paresDicom function and get back a DataSet object with the contents
// Invoke the parseDicom function and get back a DataSet object with the contents
var dataSet;
try {
var start = new Date().getTime();
dataSet = dicomParser.parseDicom(byteArray);
dataSet = dicomParser.parseDicom(byteArray, { vrCallback(tag) {
const formatted = `(${tag.substr(1, 4).toUpperCase()},${tag.substr(5,9).toUpperCase()})`;
return !!TAG_DICT[formatted] ? TAG_DICT[formatted].vr : undefined;
}});

var options = {
omitPrivateAttibutes :false ,
Expand Down
12 changes: 9 additions & 3 deletions examples/explicitDataSetToJson/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ <h1>DICOM Dump to JSON</h1>
Drag and drop a DICOM Part 10 file into the light gray region below for a json version of it
</p>
<p>
This example illustrates how to generate a JSON string from a DICOM SOP Instance.
This example illustrates how to generate a JSON string from a DICOM SOP Instance. This example parses using
a data dictionary via the vrCallback option in order to use explicitDataSetToJS even with implicit
VR data sets.
Note that DICOM files dropped here are not uploaded anywhere, all processing
is done inside your web browser in Javascript.
</p>
Expand Down Expand Up @@ -65,6 +67,7 @@ <h1>DICOM Dump to JSON</h1>

<!-- include the dicomParser library -->
<script src="../../dist/dicomParser.js"></script>
<script src="../dataDictionary.js"></script>
<script>window.dicomParser || document.write('<script src="https://unpkg.com/dicom-parser">\x3C/script>')</script>

<!-- jquery - included to make things easier to demo, not needed by dicomParser -->
Expand Down Expand Up @@ -102,11 +105,14 @@ <h1>DICOM Dump to JSON</h1>
// set a short timeout to do the parse so the DOM has time to update itself with the above message
setTimeout(function() {

// Invoke the paresDicom function and get back a DataSet object with the contents
// Invoke the parseDicom function and get back a DataSet object with the contents
var dataSet;
try {
var start = new Date().getTime();
dataSet = dicomParser.parseDicom(byteArray);
dataSet = dicomParser.parseDicom(byteArray, { vrCallback(tag) {
const formatted = `(${tag.substr(1, 4).toUpperCase()},${tag.substr(5,9).toUpperCase()})`;
return !!TAG_DICT[formatted] ? TAG_DICT[formatted].vr : undefined;
}});

var options = {
omitPrivateAttibutes :false ,
Expand Down
19 changes: 9 additions & 10 deletions src/readDicomElementImplicit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@ import { isPrivateTag } from './util/util.js';
* Internal helper functions for for parsing DICOM elements
*/

const isSequence = (element, byteStream, vrCallback) => {
// if a data dictionary callback was provided, use that to verify that the element is a sequence.
if (vrCallback !== undefined) {
const callbackValue = vrCallback(element.tag);
if (callbackValue !== undefined) {
return (callbackValue === 'SQ');
}
const isSequence = (element, byteStream) => {
if (element.vr !== undefined) {
return (element.vr === 'SQ');
}

if ((byteStream.position + 4) <= byteStream.byteArray.length) {
Expand All @@ -38,8 +34,11 @@ export default function readDicomElementImplicit (byteStream, untilTag, vrCallba
throw 'dicomParser.readDicomElementImplicit: missing required parameter \'byteStream\'';
}

const tag = readTag(byteStream);

const element = {
tag: readTag(byteStream),
tag,
vr: (vrCallback !== undefined ? vrCallback(tag) : undefined),
length: byteStream.readUint32(),
dataOffset: byteStream.position
};
Expand All @@ -53,9 +52,9 @@ export default function readDicomElementImplicit (byteStream, untilTag, vrCallba
}

// always parse sequences with undefined lengths, since there's no other way to know how long they are.
if (isSequence(element, byteStream, vrCallback) && (!isPrivateTag(element.tag) || element.hadUndefinedLength)) {
if (isSequence(element, byteStream) && (!isPrivateTag(element.tag) || element.hadUndefinedLength)) {
// parse the sequence
readSequenceItemsImplicit(byteStream, element);
readSequenceItemsImplicit(byteStream, element, vrCallback);

if (isPrivateTag(element.tag)) {
element.items = undefined;
Expand Down
1 change: 1 addition & 0 deletions test/parseDicomDataSet_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ describe('parseDicomDataSet', () => {

expect(element).to.be.ok;
expect(element.items).to.be.undefined;
expect(element.vr).to.equal('OW');
expect(element.length).to.equal(8);
});

Expand Down
2 changes: 2 additions & 0 deletions test/readDicomElementImplicit_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ describe('readDicomElementImplicit', () => {
// Assert
expect(element.tag).to.equal('x7fe00010');
expect(element.items).to.equal(undefined);
expect(element.vr).to.equal('OW');
expect(element.length).to.equal(8);
});

Expand Down Expand Up @@ -235,6 +236,7 @@ describe('readDicomElementImplicit', () => {
// Assert
expect(element.tag).to.equal('x7fe00010');
expect(element.items).to.equal(undefined);
expect(element.vr).to.equal('OW');
expect(element.length).to.equal(11);
});

Expand Down
3 changes: 3 additions & 0 deletions test/readSequenceItemsImplicit_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('readSequenceItemsImplicit', () => {

expect(pixelData).to.be.ok;
expect(pixelData.length).to.equal(8);
expect(pixelData.vr).to.equal('OW');
expect(sequenceItem.dataSet.elements['xfffee00d']).to.be.ok;
expect(byteStream.warnings.length).to.equal(0);
});
Expand Down Expand Up @@ -115,6 +116,7 @@ describe('readSequenceItemsImplicit', () => {

expect(pixelData).to.be.ok;
expect(pixelData.length).to.equal(8);
expect(pixelData.vr).to.equal('OW');
expect(byteStream.warnings.length).to.equal(0);
});

Expand Down Expand Up @@ -148,6 +150,7 @@ describe('readSequenceItemsImplicit', () => {

expect(pixelData).to.be.ok;
expect(pixelData.length).to.equal(8);
expect(pixelData.vr).to.equal('OW');
expect(byteStream.warnings.length).to.equal(0);
});

Expand Down

0 comments on commit 98e991d

Please sign in to comment.