Skip to content

Examples

Pantelis Georgiadis edited this page Aug 7, 2024 · 1 revision

Convert DICOM to JPEG

const dcmjsImaging = require('dcmjs-imaging');
const { DicomImage, NativePixelDecoder } = dcmjsImaging;
const jpeg = require('jpeg-js');
const fs = require('fs');

// Register native decoders WebAssembly to enable compressed dataset rendering.
// This needs to be performed once and not on every rendering call.
await NativePixelDecoder.initializeAsync();

// Load your DICOM dataset into a buffer from a file/db/blob.
const dicomFileBuffer = fs.readFileSync('DICOM_FILE.DCM');

// Read buffer into a DicomImage.
const image = new DicomImage(
  dicomFileBuffer.buffer.slice(
    dicomFileBuffer.byteOffset,
    dicomFileBuffer.byteOffset + dicomFileBuffer.byteLength
  )
);

// Render DicomImage, using the default parameters.
const renderingResult = image.render();

// Compress the rendered pixels into a JPEG-encoded buffer.
const jpegBuffer = jpeg.encode(
  {
    data: Buffer.from(renderingResult.pixels),
    width: renderingResult.width,
    height: renderingResult.height,
  },
  100
).data;

// Save the resulting JPEG buffer to file/db/blob.
fs.writeFileSync('JPEG_FILE.JPG', jpegBuffer);

Convert DICOM to PNG

const dcmjsImaging = require('dcmjs-imaging');
const { DicomImage, NativePixelDecoder } = dcmjsImaging;
const png = require('fast-png');
const fs = require('fs');

// Register native decoders WebAssembly to enable compressed dataset rendering.
// This needs to be performed once and not on every rendering call.
await NativePixelDecoder.initializeAsync();

// Load your DICOM dataset into a buffer from a file/db/blob.
const dicomFileBuffer = fs.readFileSync('DICOM_FILE.DCM');

// Read buffer into a DicomImage.
const image = new DicomImage(
  dicomFileBuffer.buffer.slice(
    dicomFileBuffer.byteOffset,
    dicomFileBuffer.byteOffset + dicomFileBuffer.byteLength
  )
);

// Render DicomImage, using the default parameters.
const renderingResult = image.render();

// Compress the rendered pixels into a PNG-encoded buffer.
const pngBuffer = png.encode({
  data: Buffer.from(renderingResult.pixels),
  width: renderingResult.width,
  height: renderingResult.height,
});

// Save the resulting PNG buffer to file/db/blob.
fs.writeFileSync('PNG_FILE.PNG', pngBuffer);
Clone this wiki locally