Skip to content

Commit

Permalink
fix improves the speed of writing bytes for an encapsulated multifram…
Browse files Browse the repository at this point in the history
…e image (#159) (#160)

* fix(writeBytes is crashing the browser when using encapsulated files with lots of frames): improves the speed to write bytes for a multiframe image (#159)

* optimize the writeBytes method by preventing too many buffer allocations

* fix: DicomDict.write fails when a Sequence tag a OW type. (#161)
  • Loading branch information
WoonchanCho committed Jan 22, 2021
1 parent 7f16bc5 commit b77c642
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
24 changes: 18 additions & 6 deletions src/BufferStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,24 @@ class BufferStream {
}

concat(stream) {
var newbuf = new ArrayBuffer(this.offset + stream.size),
int8 = new Uint8Array(newbuf);
int8.set(new Uint8Array(this.getBuffer(0, this.offset)));
int8.set(new Uint8Array(stream.getBuffer(0, stream.size)), this.offset);
this.buffer = newbuf;
this.view = new DataView(this.buffer);
var available = this.buffer.byteLength - this.offset;
if (stream.size > available) {
let newbuf = new ArrayBuffer(this.offset + stream.size);
let int8 = new Uint8Array(newbuf);
int8.set(new Uint8Array(this.getBuffer(0, this.offset)));
int8.set(
new Uint8Array(stream.getBuffer(0, stream.size)),
this.offset
);
this.buffer = newbuf;
this.view = new DataView(this.buffer);
} else {
let int8 = new Uint8Array(this.buffer);
int8.set(
new Uint8Array(stream.getBuffer(0, stream.size)),
this.offset
);
}
this.offset += stream.size;
this.size = this.offset;
return this.buffer.byteLength;
Expand Down
25 changes: 22 additions & 3 deletions src/ValueRepresentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,22 @@ class BinaryRepresentation extends ValueRepresentation {
frames = value.length,
startOffset = [];

// Calculate a total length for storing binary stream
var bufferLength = 0;
for (i = 0; i < frames; i++) {
bufferLength += value[i].byteLength;
let fragmentsLength = 1;
if (fragmentMultiframe) {
fragmentsLength = Math.ceil(
value[i].byteLength / fragmentSize
);
}
// 8 bytes per fragment are needed to store 0xffff (2 bytes), 0xe000 (2 bytes), and frageStream size (4 bytes)
bufferLength += fragmentsLength * 8;
}

binaryStream = new WriteBufferStream(
1024 * 1024 * 20,
bufferLength,
stream.isLittleEndian
);

Expand Down Expand Up @@ -764,7 +778,7 @@ class SequenceOfItems extends ValueRepresentation {
}
}

writeBytes(stream, value, syntax) {
writeBytes(stream, value, syntax, writeOptions) {
let written = 0;

if (value) {
Expand All @@ -774,7 +788,12 @@ class SequenceOfItems extends ValueRepresentation {
super.write(stream, "Uint16", 0xe000);
super.write(stream, "Uint32", 0xffffffff);

written += DicomMessage.write(item, stream, syntax);
written += DicomMessage.write(
item,
stream,
syntax,
writeOptions
);

super.write(stream, "Uint16", 0xfffe);
super.write(stream, "Uint16", 0xe00d);
Expand Down

0 comments on commit b77c642

Please sign in to comment.