Skip to content

Commit

Permalink
fix(data): UN VR setting during naturalization/denaturalization (#379)
Browse files Browse the repository at this point in the history
* FIx UN vr denaturalization

* Update tests

* Revert saving UN vr in _vrMap

* Add parsing from UN to dictionary vr

* Remove parseUnknownVr flag

* FIx test for UN vr

* Fix space

* Fix UN vr test formatting
  • Loading branch information
andreidubov authored Mar 5, 2024
1 parent e84a36c commit 1fc8ad8
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/DicomMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,19 @@ class DicomMessage {
vr = ValueRepresentation.createByTypeString(vrType);
} else {
vrType = stream.readVR();
vr = ValueRepresentation.createByTypeString(vrType);

if (
vrType === "UN" &&
DicomMessage.lookupTag(tag) &&
DicomMessage.lookupTag(tag).vr
) {
vrType = DicomMessage.lookupTag(tag).vr;

vr = ValueRepresentation.parseUnknownVr(vrType);
} else {
vr = ValueRepresentation.createByTypeString(vrType);
}

if (vr.isExplicit()) {
stream.increment(2);
length = stream.readUint32();
Expand Down
47 changes: 47 additions & 0 deletions src/ValueRepresentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ class ValueRepresentation {
}
return vr;
}

static parseUnknownVr(type) {
return new ParsedUnknownValue(type);
}
}

class AsciiStringRepresentation extends ValueRepresentation {
Expand Down Expand Up @@ -1213,6 +1217,49 @@ class UnknownValue extends BinaryRepresentation {
}
}

class ParsedUnknownValue extends BinaryRepresentation {
constructor(vr) {
super(vr);
this.maxLength = null;
this.padByte = 0;
this.noMultiple = true;
this._isBinary = true;
this._allowMultiple = false;
this._isExplicit = true;
}

read(stream, length, syntax) {
const arrayBuffer = this.readBytes(stream, length, syntax)[0];
const streamFromBuffer = new ReadBufferStream(arrayBuffer, true);
const vr = ValueRepresentation.createByTypeString(this.type);

var values = [];
if (vr.isBinary() && length > vr.maxLength && !vr.noMultiple) {
var times = length / vr.maxLength,
i = 0;
while (i++ < times) {
values.push(vr.read(streamFromBuffer, vr.maxLength, syntax));
}
} else {
var val = vr.read(streamFromBuffer, length, syntax);
if (!vr.isBinary() && singleVRs.indexOf(vr.type) == -1) {
values = val;
if (typeof val === "string") {
values = val.split(String.fromCharCode(VM_DELIMITER));
}
} else if (vr.type == "SQ") {
values = val;
} else if (vr.type == "OW" || vr.type == "OB") {
values = val;
} else {
Array.isArray(val) ? (values = val) : values.push(val);
}
}

return values;
}
}

class OtherWordString extends BinaryRepresentation {
constructor() {
super("OW");
Expand Down
24 changes: 24 additions & 0 deletions test/data.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,30 @@ describe("The same DICOM file loaded from both DCM and JSON", () => {
});
});

describe("test_un_vr", () => {
it("Tag with UN vr should be parsed according VR in dictionary", async () => {
const expectedExposureIndex = 662;
const expectedDeviationIndex = -1.835;

const url = "https://github.com/dcmjs-org/data/releases/download/unknown-VR/sample-dicom-with-un-vr.dcm";
const dcmPath = await getTestDataset(url, "sample-dicom-with-un-vr.dcm");

const file = await promisify(fs.readFile)(dcmPath);
const dicomData = dcmjs.data.DicomMessage.readFile(file.buffer, {
ignoreErrors: false,
untilTag: null,
includeUntilTagValue: false,
noCopy: false,
});
const dataset = dcmjs.data.DicomMetaDictionary.naturalizeDataset(
dicomData.dict
);

expect(dataset.ExposureIndex).toEqual(expectedExposureIndex);
expect(dataset.DeviationIndex).toEqual(expectedDeviationIndex);
});
});

it.each([
[1.0, "1"],
[0.0, "0"],
Expand Down

0 comments on commit 1fc8ad8

Please sign in to comment.