question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting: Error parsing byte stream RangeError: Invalid typed array length: ####

See original GitHub issue

I’m trying to use the simple example to read in a dicom file and display it on the page using cornerstone and the cornerstoneWADOImageLoader libraries. I have this working fine using regular files and the wado fileuri loader, but I’m trying to get it working with byte arrays so I can do work with it with server-processed data. The simple example is throwing an error based on an incorrect Uint16Array length, I believe. Does anyone see anything wrong with this code?

const reader = new FileReader();
reader.addEventListener('load', (e) => {
    let pixelData = self.loadP10Data(reader.result);
    dicomImageData['example://1'] = pixelData;
    self.loadAndViewP10Image('example://1');
}, false);

reader.readAsArrayBuffer(file);

loadP10Data(readerResult) {
    const self = this;
    try
    {
        let byteArray = new Uint8Array(readerResult);

        // Parse the byte array to get a DataSet object that has the parsed contents
        var dataSet = dicomParser.parseDicom(byteArray/*, options */);

        // get the pixel data element (contains the offset and length of the data)
        var pixelDataElement = dataSet.elements.x7fe00010;

        // create a typed array on the pixel data (this example assumes 16 bit unsigned data)
        // FAILS HERE:
        var pixelData = new Uint16Array(dataSet.byteArray.buffer, pixelDataElement.dataOffset, pixelDataElement.length);

        return pixelData;
    }
    catch(ex)
    {
        console.log('Error parsing byte stream', ex);
    }
    return '';
}

Error:

Got pixel data {tag: “x7fe00010”, vr: “OW”, length: 89456, dataOffset: 2098, hadUndefinedLength: true, …} app.js:56571 LENGTH 89456 app.js:56577 Error parsing byte stream RangeError: Invalid typed array length: 89456 at typedArrayConstructByArrayBuffer (<anonymous>) at new Uint16Array (native) at StringBase64DicomFile.loadP10Data (http://localhost:3000/app.js:56573:29) at FileReader.reader.addEventListener (http://localhost:3000/app.js:56539:26)

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
yagnicommented, Jul 13, 2018

I can’t speak to the cornerstone side, but the RangeError is most likely coming from using the length in the UInt16Array constructor. It should be the number of elements, whereas the length field from the data element is the number of bytes. So this should be more accurate, assuming each pixel is 2 bytes:

var pixelData = new Uint16Array(dataSet.byteArray.buffer, pixelDataElement.dataOffset, pixelDataElement.length / 2);

Also remember to take the BitsStored field into account: if it’s 8, you’ll need to put the pixels in a UInt8Array; if it’s 16, UInt16Array works.

1reaction
dannyrbcommented, Feb 9, 2018

Thanks for posting your solution, @rw3iss!

@swederik is this within your realm of expertise? This doesn’t sound like supported functionality, but I’ve seen similar requests floating around. It might be worth marking it as an enhancement? How hard would it be to create a custom loader that takes a “key” that corresponds to a local store, and grabs the value (byte array) and successfully creates a new image object w/ it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error parsing byte stream RangeError: Invalid typed array ...
I'm trying to use the simple example to read in a dicom file and display it on the page using cornerstone and the...
Read more >
RangeError: invalid array length - JavaScript - MDN Web Docs
The JavaScript exception "Invalid array length" occurs when specifying an array length that is either negative, a floating number or exceeds ...
Read more >
"RangeError: Invalid typed array length" for seemingly-valid ...
When called with a length argument, an internal array buffer is created in memory of size length multiplied by BYTES_PER_ELEMENT bytes ...
Read more >
RangeError: invalid array length - JavaScript
What went wrong? · When creating an Array or an ArrayBuffer which has a length which is either negative or larger or equal...
Read more >
Node.js v19.3.0 Documentation
arrayBuffer (); blob.size; blob.slice([start[, end[, type]]]); blob.stream() ... Settings object; Error handling; Invalid character handling in header names ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found