ivmartel / dwv

DICOM Web Viewer: open source zero footprint medical image library.
https://ivmartel.github.io/dwv/
GNU General Public License v3.0
1.68k stars 593 forks source link

Error parsing image - nested sequences? #180

Closed bisol closed 9 years ago

bisol commented 9 years ago

I'm using DWV 0.11.1, and got this error while trying to display an image:

Error: Missing or empty DICOM image number of columns

I've traced it to these tests on dwv.dicom.DicomParser.prototype.parse

// end of sequence with explicit length
if ( dataElement.vr !== "SQ" && sequences.length !== 0 ) {

Seems like it can't handle a sequence with another sequence as it's last item.

This site limits attachments by file extension, so I uploaded the DICOM file renamed to png :p

6809 anon

bisol commented 9 years ago

I have tried and failed to commit a new branch. Anyhow, this worked for me:

        // handle nested sequences of explicit length
        if (dataElement.vr == "SQ" && sequences.length > 1 ) {
            sequences[sequences.length - 2].vlCount -= 4;  // offsets the +12 below 
        }

        // end of sequence with explicit length
        if ( dataElement.vr !== "SQ" && sequences.length !== 0 ) {
            var last = sequences.length - 1;
            sequences[last].vlCount += tagOffset;
            // check if we have reached the sequence vl
            while ( sequences.length > 0 && sequences[last].vlCount === sequences[last].vl ) {
                // last count + size of a sequence
                var lastVlCount = sequences[last].vlCount + 12;
                // remove last sequence
                sequences = sequences.slice(0, -1);
                // add nested sequence vl
                if ( sequences.length !== 0 ) {
                    last = sequences.length - 1;
                    sequences[last].vlCount += lastVlCount;
                }
            }
        }
pervez8ktt commented 9 years ago

there are so many problems with current dicomParser.js

you need to update dicomParser.js with new one that is

https://github.com/chafey/dicomParser

ivmartel commented 9 years ago

Thanks for your code @bisol!

ivmartel commented 9 years ago

Your comment is not really helping the issue @pervez8ktt... Feel free to contribute to the dcmbench project to prove your point!

bisol commented 9 years ago

@ivmartel
I should mention that I don't fully understand the magic numbers (+12 and -4). I tested this with a handful of DICOM images, but that's far from thorough. Maybe it will break with a triple nested sequence...

ivmartel commented 9 years ago

Ok, got it, the problem is implicit/explicit transfer syntax. The 4 bytes are used to store the VR in the explicit case (see DICOM sect_7.5.html#table_7.5-1) which was what I tested against. Yours is implicit so no need for those 4 bytes.