Skip to content

Commit

Permalink
feat: add dcm4che tool (uft8 converter, dcm2jpg)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chinlinlee committed Mar 29, 2023
1 parent ad1f4d1 commit 730e0c0
Show file tree
Hide file tree
Showing 2,086 changed files with 188,809 additions and 4 deletions.
1 change: 1 addition & 0 deletions models/DICOM/dcm4che/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.dcm filter=lfs diff=lfs merge=lfs -text
1 change: 1 addition & 0 deletions models/DICOM/dcm4che/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
125 changes: 125 additions & 0 deletions models/DICOM/dcm4che/Dcm2Jpeg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
const _ = require("lodash");
const fs = require("fs");
const { java } = require("./java-instance");
const { File } = require("./wrapper/java/io/File");
const { Dcm2Jpg } = require("./wrapper/org/dcm4che3/tool/dcm2jpg/Dcm2Jpg");
const { createDcm2Jpg$ReadImageProxy } = require("./wrapper/org/dcm4che3/tool/dcm2jpg/Dcm2Jpg$ReadImage");
const { ICCProfile$Option } = require("./wrapper/org/dcm4che3/image/ICCProfile$Option");
const { Float } = require("./wrapper/java/lang/Float");

/**
* @typedef Dcm2JpgOptions
* @property {string} dicomFile
* @property {string} jpgFile
* @property {string} iccProfileName
* @property {number} frameNumber
* @property {number} jpgQuality
* @property {number} windowCenter
* @property {number} windowWidth
* @property {string} format
*/


class JsDcm2Jpeg {

/** @type {Dcm2JpgOptions} */
static defaultOptions={ format: "JPEG", jpgQuality: 0.9, frameNumber: 1 };

/**
*
* @param {Dcm2JpgOptions} options
*/
constructor(options = { format: "JPEG", jpgQuality: 0.9, frameNumber: 1 }) {
/** @type {Dcm2Jpg} */
this.dcm2jpg;

this.options = options;
}

async init() {
this.dcm2jpg = await Dcm2Jpg.newInstanceAsync();

const readImageProxy = createDcm2Jpg$ReadImageProxy({
apply: (file) => {
return this.dcm2jpg.readImageFromDicomInputStreamSync(file);
}
}, {
keepAsDaemon: true
});

await this.dcm2jpg.setReadImage(readImageProxy);
await this.dcm2jpg.setICCProfile(await ICCProfile$Option.valueOf("no"));
await this.dcm2jpg.setFrame(this.options.frameNumber);

await this.initSetWindowCenter();
await this.initSetWindowWidth();

await this.dcm2jpg.initImageWriter(this.options.format,
null,
"com.sun.imageio.plugins.*",
null,
await Float.newInstanceAsync(this.options.jpgQuality)
);

readImageProxy.reset();

return this;
}

async convert(src, dest) {

if (_.isString(src)) {
src = await File.newInstanceAsync(src);
}

if (_.isString(dest)) {
dest = await File.newInstanceAsync(dest);
}

await this.dcm2jpg.convert(src, dest);
}

/**
* @private
*/
async initSetWindowWidth() {
if (this.options.windowWidth) {
await this.dcm2jpg.setWindowWidth(this.options.windowWidth);
}
}

/**
* @private
*/
async initSetWindowCenter() {
if (this.options.windowCenter) {
await this.dcm2jpg.setWindowCenter(this.options.windowCenter);
}
}

async getFrameImage(imagesPath , frameNumber) {
let jpegFile = imagesPath.replace(/\.dcm\b/gi , `.${frameNumber-1}.jpg`);

try {
await this.dcm2jpg.setFrame(frameNumber);

await this.convert(imagesPath, jpegFile);

let rs = fs.createReadStream(jpegFile);
return {
status : true ,
imageStream : rs,
imagePath: jpegFile
};
} catch(e) {
console.error(e);
return {
status : false ,
imageStream : e,
imagePath: jpegFile
};
}
}
}

module.exports.JsDcm2Jpeg = JsDcm2Jpeg;
63 changes: 63 additions & 0 deletions models/DICOM/dcm4che/DicomUtf8Converter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const { java } = require("./java-instance");
const { File } = require("./wrapper/java/io/File");
const { ElementDictionary } = require("./wrapper/org/dcm4che3/data/ElementDictionary");
const { Tag } = require("./wrapper/org/dcm4che3/data/Tag");
const { Attributes } = require("./wrapper/org/dcm4che3/data/Attributes");
const { DicomInputStream } = require("./wrapper/org/dcm4che3/io/DicomInputStream");
const { DicomOutputStream } = require("./wrapper/org/dcm4che3/io/DicomOutputStream");
const { SafeClose } = require("./wrapper/org/dcm4che3/util/SafeClose");


class DicomUtf8Converter {
constructor(filename, outputFilename = "") {
this.filename = filename;
this.outputFilename = outputFilename;
if (!outputFilename) this.outputFilename = this.filename;

/** @type {Attributes} */
this.attributes;
/** @type {Attributes} */
this.fileMetaInfo;
}

/**
* @private
*/
async readDicomFile() {
let file = await File.newInstanceAsync(this.filename);

/** @type {DicomInputStream} */
let dicomInputStream;
try {

dicomInputStream = await DicomInputStream.newInstanceAsync(file);

this.attributes = await dicomInputStream.readDataset();

this.fileMetaInfo = await dicomInputStream.readFileMetaInformation();
this.fileMetaInfo = await this.attributes.createFileMetaInformation(await dicomInputStream.getTransferSyntax());
} finally {
dicomInputStream.closeSync();
}
}

async convert() {
await this.readDicomFile();

await this.attributes.setString(Tag.SpecificCharacterSet, ElementDictionary.vrOfSync(Tag.SpecificCharacterSet, ""), "ISO_IR 192");

let outputFile = new File(this.outputFilename);

/** @type {DicomOutputStream} */
let dicomOutputStream;
try {
dicomOutputStream = await DicomOutputStream.newInstanceAsync(outputFile);
await dicomOutputStream.writeDataset(this.fileMetaInfo, this.attributes);
} finally {
SafeClose.closeSync(dicomOutputStream);
}
}

}

module.exports.DicomUtf8Converter = DicomUtf8Converter;
14 changes: 14 additions & 0 deletions models/DICOM/dcm4che/java-instance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const java = require('java-bridge');
const glob = require("glob");
const path = require("path");

java.ensureJvm();

let jarFiles = glob.sync("**/*.jar", {
cwd: path.join(__dirname, "./javaNode"),
absolute: true
});

java.appendClasspath(jarFiles);

module.exports.java = java;
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 730e0c0

Please sign in to comment.