Skip to content

Commit

Permalink
feat(VR) Enable a force write option for files with abnormal VRs #162 (
Browse files Browse the repository at this point in the history
…#186)

- add allowInvalidVRLength (false by default) in the write options
- add a test case for testing this option
  • Loading branch information
WoonchanCho authored Mar 4, 2021
1 parent b17ea9a commit f07bb82
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 179 deletions.
5 changes: 3 additions & 2 deletions src/DicomDict.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DicomDict {
}
}

write(writeOptions = {}) {
write(writeOptions = { allowInvalidVRLength: false }) {
var metaSyntax = EXPLICIT_LITTLE_ENDIAN;
var fileStream = new WriteBufferStream(4096, true);
fileStream.writeHex("00".repeat(128));
Expand All @@ -36,7 +36,8 @@ class DicomDict {
"00020000",
"UL",
metaStream.size,
metaSyntax
metaSyntax,
writeOptions
);
fileStream.concat(metaStream);

Expand Down
4 changes: 2 additions & 2 deletions src/DicomMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ class DicomMessage {
return dicomDict;
}

static writeTagObject(stream, tagString, vr, values, syntax) {
static writeTagObject(stream, tagString, vr, values, syntax, writeOptions) {
var tag = Tag.fromString(tagString);

tag.write(stream, vr, values, syntax);
tag.write(stream, vr, values, syntax, writeOptions);
}

static write(jsonObjects, useStream, syntax, writeOptions) {
Expand Down
4 changes: 3 additions & 1 deletion src/Tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,15 @@ class Tag {
isEncapsulated,
writeOptions
);
} else {
} else if (vrType == "SQ") {
valueLength = vr.writeBytes(
tagStream,
values,
useSyntax,
writeOptions
);
} else {
valueLength = vr.writeBytes(tagStream, values, writeOptions);
}

if (vrType == "SQ") {
Expand Down
82 changes: 51 additions & 31 deletions src/ValueRepresentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,13 @@ class ValueRepresentation {
}
}

writeBytes(stream, value, lengths) {
writeBytes(
stream,
value,
lengths,
writeOptions = { allowInvalidVRLength: false }
) {
const { allowInvalidVRLength } = writeOptions;
var valid = true,
valarr = Array.isArray(value) ? value : [value],
total = 0;
Expand All @@ -131,7 +137,7 @@ class ValueRepresentation {
checklen = lengths[i],
isString = false,
displaylen = checklen;
if (checkValue === null) {
if (checkValue === null || allowInvalidVRLength) {
valid = true;
} else if (this.checkLength) {
valid = this.checkLength(checkValue);
Expand All @@ -144,14 +150,14 @@ class ValueRepresentation {
valid = checklen <= this.maxLength;
}

var errmsg =
"Value exceeds max length, vr: " +
this.type +
", value: " +
checkValue +
", length: " +
displaylen;
if (!valid) {
var errmsg =
"Value exceeds max length, vr: " +
this.type +
", value: " +
checkValue +
", length: " +
displaylen;
if (isString) log.log(errmsg);
else throw new Error(errmsg);
}
Expand Down Expand Up @@ -228,10 +234,12 @@ class StringRepresentation extends ValueRepresentation {
return stream.readString(length);
}

writeBytes(stream, value) {
writeBytes(stream, value, writeOptions) {
// TODO will delete
if (!writeOptions) throw new Error("writeOptions is undefined");
const written = super.write(stream, "String", value);

return super.writeBytes(stream, value, written);
return super.writeBytes(stream, value, written, writeOptions);
}
}

Expand Down Expand Up @@ -321,7 +329,12 @@ class BinaryRepresentation extends ValueRepresentation {
var binaryData = value[0];
binaryStream = new ReadBufferStream(binaryData);
stream.concat(binaryStream);
return super.writeBytes(stream, binaryData, [binaryStream.size]);
return super.writeBytes(
stream,
binaryData,
[binaryStream.size],
writeOptions
);
}
}

Expand Down Expand Up @@ -455,11 +468,12 @@ class AttributeTag extends ValueRepresentation {
return tagFromNumbers(group, element).value;
}

writeBytes(stream, value) {
writeBytes(stream, value, writeOptions) {
return super.writeBytes(
stream,
value,
super.write(stream, "TwoUint16s", value)
super.write(stream, "TwoUint16s", value),
writeOptions
);
}
}
Expand Down Expand Up @@ -497,9 +511,9 @@ class DecimalString extends StringRepresentation {
return ds;
}

writeBytes(stream, value) {
writeBytes(stream, value, writeOptions) {
const val = Array.isArray(value) ? value.map(String) : [value];
return super.writeBytes(stream, val);
return super.writeBytes(stream, val, writeOptions);
}
}

Expand All @@ -524,11 +538,12 @@ class FloatingPointSingle extends ValueRepresentation {
return Number(stream.readFloat());
}

writeBytes(stream, value) {
writeBytes(stream, value, writeOptions) {
return super.writeBytes(
stream,
value,
super.write(stream, "Float", value)
super.write(stream, "Float", value),
writeOptions
);
}
}
Expand All @@ -546,11 +561,12 @@ class FloatingPointDouble extends ValueRepresentation {
return Number(stream.readDouble());
}

writeBytes(stream, value) {
writeBytes(stream, value, writeOptions) {
return super.writeBytes(
stream,
value,
super.write(stream, "Double", value)
super.write(stream, "Double", value),
writeOptions
);
}
}
Expand Down Expand Up @@ -579,9 +595,9 @@ class IntegerString extends StringRepresentation {
return is;
}

writeBytes(stream, value) {
writeBytes(stream, value, writeOptions) {
const val = Array.isArray(value) ? value.map(String) : [value];
return super.writeBytes(stream, val);
return super.writeBytes(stream, val, writeOptions);
}
}

Expand Down Expand Up @@ -672,11 +688,12 @@ class SignedLong extends ValueRepresentation {
return stream.readInt32();
}

writeBytes(stream, value) {
writeBytes(stream, value, writeOptions) {
return super.writeBytes(
stream,
value,
super.write(stream, "Int32", value)
super.write(stream, "Int32", value),
writeOptions
);
}
}
Expand Down Expand Up @@ -806,7 +823,7 @@ class SequenceOfItems extends ValueRepresentation {
super.write(stream, "Uint32", 0x00000000);
written += 8;

return super.writeBytes(stream, value, [written]);
return super.writeBytes(stream, value, [written], writeOptions);
}
}

Expand All @@ -824,11 +841,12 @@ class SignedShort extends ValueRepresentation {
return stream.readInt16();
}

writeBytes(stream, value) {
writeBytes(stream, value, writeOptions) {
return super.writeBytes(
stream,
value,
super.write(stream, "Int16", value)
super.write(stream, "Int16", value),
writeOptions
);
}
}
Expand Down Expand Up @@ -897,11 +915,12 @@ class UnsignedShort extends ValueRepresentation {
return stream.readUint16();
}

writeBytes(stream, value) {
writeBytes(stream, value, writeOptions) {
return super.writeBytes(
stream,
value,
super.write(stream, "Uint16", value)
super.write(stream, "Uint16", value),
writeOptions
);
}
}
Expand All @@ -919,11 +938,12 @@ class UnsignedLong extends ValueRepresentation {
return stream.readUint32();
}

writeBytes(stream, value) {
writeBytes(stream, value, writeOptions) {
return super.writeBytes(
stream,
value,
super.write(stream, "Uint32", value)
super.write(stream, "Uint32", value),
writeOptions
);
}
}
Expand Down
Binary file added test/invalid-vr-length-test.dcm
Binary file not shown.
Loading

0 comments on commit f07bb82

Please sign in to comment.