Skip to content

Commit

Permalink
feat(deflated): Added support for reading datasets with deflated tran…
Browse files Browse the repository at this point in the history
…sfer syntax (#312)

* feat(deflated): Added support for reading datasets with deflated transfer syntax

* Updated deflated tests
  • Loading branch information
PantelisGeorgiadis authored Sep 20, 2022
1 parent 6039905 commit 914b3dc
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ _Parts of DICOM that dcmjs *will* focus on:_

_Parts of DICOM that dcmjs *will not* focus on:_

- DIMSE (legacy networking like C-STORE, C-FIND, C-MOVE, etc). See the [dcmjs-dimse project](https://github.com/PantelisGeorgiadis/dcmjs-dimse) for that.
- DIMSE (legacy networking like C-STORE, C-FIND, C-MOVE, etc). See the [dcmjs-dimse](https://github.com/PantelisGeorgiadis/dcmjs-dimse) project for that.
- Physical Media (optical disks). See [this FAQ](https://www.dclunie.com/medical-image-faq/html/index.html) if you need to work with those.
- Image processing. See [dcmjs-imaging](https://github.com/PantelisGeorgiadis/dcmjs-imaging) for this.
- Image rendering. See [dcmjs-imaging](https://github.com/PantelisGeorgiadis/dcmjs-imaging) for this.

# Usage

Expand Down
13 changes: 12 additions & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
"gl-matrix": "^3.1.0",
"lodash.clonedeep": "^4.5.0",
"loglevelnext": "^3.0.1",
"ndarray": "^1.0.19"
"ndarray": "^1.0.19",
"pako": "^2.0.4"
},
"husky": {
"hooks": {
Expand Down
12 changes: 12 additions & 0 deletions src/BufferStream.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pako from "pako";

function toInt(val) {
if (isNaN(val)) {
throw new Error("Not a number: " + val);
Expand Down Expand Up @@ -423,6 +425,15 @@ class ReadBufferStream extends BufferStream {
}
}

class DeflatedReadBufferStream extends ReadBufferStream {
constructor(stream, options) {
const inflatedBuffer = pako.inflateRaw(
stream.getBuffer(stream.offset, stream.size)
);
super(inflatedBuffer.buffer, stream.littleEndian, options);
}
}

class WriteBufferStream extends BufferStream {
constructor(buffer, littleEndian) {
super(buffer, littleEndian);
Expand All @@ -431,4 +442,5 @@ class WriteBufferStream extends BufferStream {
}

export { ReadBufferStream };
export { DeflatedReadBufferStream };
export { WriteBufferStream };
12 changes: 11 additions & 1 deletion src/DicomMessage.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { ReadBufferStream } from "./BufferStream.js";
import { DeflatedReadBufferStream } from "./BufferStream.js";
import { Tag } from "./Tag.js";
import { DicomMetaDictionary } from "./DicomMetaDictionary.js";
import { DicomDict } from "./DicomDict.js";
import { ValueRepresentation } from "./ValueRepresentation.js";

const IMPLICIT_LITTLE_ENDIAN = "1.2.840.10008.1.2";
const EXPLICIT_LITTLE_ENDIAN = "1.2.840.10008.1.2.1";
const DEFLATED_EXPLICIT_LITTLE_ENDIAN = "1.2.840.10008.1.2.1.99";
const EXPLICIT_BIG_ENDIAN = "1.2.840.10008.1.2.2";
const singleVRs = ["SQ", "OF", "OW", "OB", "UN", "LT"];

Expand Down Expand Up @@ -203,10 +205,18 @@ class DicomMessage {

//read header buffer
var metaStream = stream.more(metaLength);

var metaHeader = DicomMessage._read(metaStream, useSyntax, options);

//get the syntax
var mainSyntax = metaHeader["00020010"].Value[0];

//in case of deflated dataset, decompress and continue
if (mainSyntax === DEFLATED_EXPLICIT_LITTLE_ENDIAN) {
stream = new DeflatedReadBufferStream(stream, {
noCopy: options.noCopy
});
}

mainSyntax = DicomMessage._normalizeSyntax(mainSyntax);
var objects = DicomMessage._read(stream, mainSyntax, options);

Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Data
import { BitArray } from "./bitArray.js";
import { ReadBufferStream } from "./BufferStream.js";
import { DeflatedReadBufferStream } from "./BufferStream.js";
import { WriteBufferStream } from "./BufferStream.js";
import { DicomDict } from "./DicomDict.js";
import { DicomMessage } from "./DicomMessage.js";
Expand Down Expand Up @@ -46,6 +47,7 @@ import { cleanTags, getTagsNameToEmpty } from "./anonymizer.js";
let data = {
BitArray,
ReadBufferStream,
DeflatedReadBufferStream,
WriteBufferStream,
DicomDict,
DicomMessage,
Expand Down
25 changes: 25 additions & 0 deletions test/data.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,31 @@ it("Writes encapsulated OB data which has an odd length with a padding byte in i
]);
});

it("test_deflated", async () => {
const url =
"https://github.com/dcmjs-org/data/releases/download/deflate-transfer-syntax/deflate_tests.zip";
const unzipPath = await getZippedTestDataset(url, "deflate_tests.zip", "deflate_tests");
const deflatedPath = path.join(unzipPath, "deflate_tests");

const expected = [
{ file: "image_dfl", tags: { Modality: "OT", Rows: 512, Columns: 512 }},
{ file: "report_dfl", tags: { Modality: "SR", VerificationFlag: "UNVERIFIED", ContentDate: "20001110" }},
{ file: "wave_dfl", tags: { Modality: "ECG", SynchronizationTrigger: "NO TRIGGER", ContentDate: "19991223" }}
];

expected.forEach(e => {
const buffer = fs.readFileSync(path.join(deflatedPath, e.file));
const dicomDict = DicomMessage.readFile(buffer.buffer.slice(
buffer.byteOffset,
buffer.byteOffset + buffer.byteLength
));
const dataset = DicomMetaDictionary.naturalizeDataset(dicomDict.dict);
Object.keys(e.tags).forEach(t => {
expect(dataset[t]).toEqual(e.tags[t]);
});
});
});

describe("With a SpecificCharacterSet tag", () => {
it("Reads a long string in the '' character set", async () => {
expect(readEncodedLongString("", [0x68, 0x69])).toEqual("hi");
Expand Down

0 comments on commit 914b3dc

Please sign in to comment.