Skip to content

Commit

Permalink
fix: Fix UN & AT VR processing logic (#167) (#168)
Browse files Browse the repository at this point in the history
* fix: Fix UN & AT VR processing logic (#167)

* Change UnknownValue (UN) to be extended from BinaryRepresentation
* Add the writeTwoUint16s method to BufferStream to handle to process AT
* Optimize the readString method of BufferStream
* Adds test case to check the equality of pre-write and post-write buffer

* adds performance test-case and removes dead code
  • Loading branch information
WoonchanCho committed Feb 8, 2021
1 parent 0ed3347 commit 7cb975a
Show file tree
Hide file tree
Showing 5 changed files with 309 additions and 235 deletions.
10 changes: 10 additions & 0 deletions packages/adapters/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 23 additions & 9 deletions packages/adapters/src/BufferStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ class BufferStream {
return this.increment(2);
}

writeTwoUint16s(value) {
this.checkSize(4);
const first = value >> 16;
const second = value & 0xffff;
this.view.setUint16(this.offset, toInt(first), this.isLittleEndian);
this.view.setUint16(
this.offset + 2,
toInt(second),
this.isLittleEndian
);
return this.increment(4);
}

writeInt16(value) {
this.checkSize(2);
this.view.setInt16(this.offset, toInt(value), this.isLittleEndian);
Expand Down Expand Up @@ -212,16 +225,17 @@ class BufferStream {
}

readString(length) {
var string = "";

var numOfMulti = length,
index = 0;
while (index++ < numOfMulti) {
var charCode = this.readUint8();
string += String.fromCharCode(charCode);
var chars = [];
var start = this.offset;
var end = this.offset + length;
if (end >= this.buffer.byteLength) {
end = this.buffer.byteLength;
}

return string;
for (let i = start; i < end; ++i) {
chars.push(String.fromCharCode(this.view.getUint8(i)));
this.increment(1);
}
return chars.join("");
}

readHex(length) {
Expand Down
2 changes: 1 addition & 1 deletion packages/adapters/src/Tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class Tag {
valueLength;
tagStream.setEndian(isLittleEndian);

if (vrType == "OW" || vrType == "OB") {
if (vrType == "OW" || vrType == "OB" || vrType == "UN") {
valueLength = vr.writeBytes(
tagStream,
values,
Expand Down
12 changes: 4 additions & 8 deletions packages/adapters/src/ValueRepresentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ class ValueRepresentation {
//byteCount++;
}
var singularArgs = [v].concat(valueArgs.slice(1));

var byteCount = func.apply(stream, singularArgs);
written.push(byteCount);
});
Expand Down Expand Up @@ -241,10 +240,11 @@ class BinaryRepresentation extends ValueRepresentation {
super(type);
}

writeBytes(stream, value, syntax, isEncapsulated, writeOptions) {
writeBytes(stream, value, syntax, isEncapsulated, writeOptions = {}) {
var i;
var binaryStream;
var { fragmentMultiframe = true } = writeOptions;
value = value === null || value === undefined ? [] : value;
if (isEncapsulated) {
var fragmentSize = 1024 * 20,
frames = value.length,
Expand Down Expand Up @@ -459,7 +459,7 @@ class AttributeTag extends ValueRepresentation {
return super.writeBytes(
stream,
value,
super.write(stream, "Uint32", value)
super.write(stream, "TwoUint16s", value)
);
}
}
Expand Down Expand Up @@ -955,17 +955,13 @@ class UniversalResource extends StringRepresentation {
}
}

class UnknownValue extends StringRepresentation {
class UnknownValue extends BinaryRepresentation {
constructor() {
super("UN");
this.maxLength = null;
this.padByte = "00";
this.noMultiple = true;
}

readBytes(stream, length) {
return stream.readString(length);
}
}

class OtherWordString extends BinaryRepresentation {
Expand Down
Loading

0 comments on commit 7cb975a

Please sign in to comment.