dcmjs-org / dcmjs

Javascript implementation of DICOM manipulation
https://dcmjs.netlify.com/
MIT License
287 stars 108 forks source link

Support for reading from a readable stream #378

Open ericlin-nz opened 6 months ago

ericlin-nz commented 6 months ago

Hi dcmjs-org,

I was wondering if there were any plans to support reading from a readable stream in the future, or if it is already possible, if you could please help point me in the right direction on how to achieve this?

Currently, I'm using dcmjs.data.DicomMessage.readFile(...) to read from an array buffer of a DICOM file read into memory. Although this does work well, I would like to be able to alleviate potential out of memory issues as I could be reading a very large DICOM file into memory, but only actually need the tags before PixelData.

Ideally, I would like to be able to pass in a readable stream and have dcmjs read up until the desired tag (similar to the readFile function) and then stop reading once dcmjs has reached the desired tag.

Example usage:

import dcmjs from "dcmjs";
import { createReadStream } from "fs";

const fileName = "example.dcm";
const readStream = createReadStream(fileName);

// Similar arguments to the readFile function
const dicomDict = dcmjs.data.DicomMessage.readStream(readStream, {
  untilTag: "7FE00010", // Skip reading PixelData
  includeUntilTagValue: false,
}

Any help would be much appreciated. Thank you!

pieper commented 5 months ago

That sounds very reasonable - I don't know that anyone has plans to work on that, but if you wanted to suggest a backwards compatible way to do that it seems like a reasonable functionality to have.

I haven't really looked closely, but the current BufferStream code is basically treating the array as a stream, so having a variant that accepts a generic stream seems like it might be just a small change. Perhaps the same could be done for writing.