-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
640 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { DicomImage, NativePixelDecoder } from './..'; | ||
|
||
import path from 'path'; | ||
import bmp from 'bmp-js'; | ||
import fs from 'fs'; | ||
|
||
async function renderToBmp(dicomFile: string, bmpFile: string) { | ||
// Register native decoders | ||
// Optionally, provide the path to WebAssembly module. | ||
// If not provided, the module is trying to be resolved within the same directory. | ||
await NativePixelDecoder.initializeAsync({ | ||
webAssemblyModulePathOrUrl: path.resolve(__dirname, '../wasm/bin/native-pixel-decoder.wasm'), | ||
}); | ||
|
||
const fileBuffer = fs.readFileSync(dicomFile); | ||
const image = new DicomImage( | ||
fileBuffer.buffer.slice(fileBuffer.byteOffset, fileBuffer.byteOffset + fileBuffer.byteLength) | ||
); | ||
|
||
const renderingResult = image.render(); | ||
const renderedPixels = Buffer.from(renderingResult.pixels); | ||
|
||
// BMP lib expects ABGR and the rendering output is RGBA | ||
const argbPixels = Buffer.alloc(4 * image.getWidth() * image.getHeight()); | ||
for (let i = 0; i < 4 * image.getWidth() * image.getHeight(); i += 4) { | ||
argbPixels[i] = renderedPixels[i + 3]; | ||
argbPixels[i + 1] = renderedPixels[i + 2]; | ||
argbPixels[i + 2] = renderedPixels[i + 1]; | ||
argbPixels[i + 3] = renderedPixels[i]; | ||
} | ||
|
||
const encodedBmp = bmp.encode({ | ||
data: argbPixels, | ||
width: image.getWidth(), | ||
height: image.getHeight(), | ||
}); | ||
|
||
fs.writeFileSync(bmpFile, encodedBmp.data); | ||
} | ||
|
||
const args = process.argv.slice(2); | ||
(async () => { | ||
await renderToBmp(args[0], args[1]); | ||
})(); |
Oops, something went wrong.