Parsing SQ with unknown length containing data elements of unknownlength - DICOM

This is a discussion on Parsing SQ with unknown length containing data elements of unknownlength - DICOM ; Hi, I'm just trying to wrap my head around how to parse DICOM data. In particular, I'm currently looking at PS 3.5-2008, Tables 7.5-{1,2,3}. The first case is easy: The sequence tells me how long it is, and every element ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: Parsing SQ with unknown length containing data elements of unknownlength

  1. Parsing SQ with unknown length containing data elements of unknownlength

    Hi,

    I'm just trying to wrap my head around how to parse DICOM data. In
    particular, I'm currently looking at PS 3.5-2008, Tables 7.5-{1,2,3}.

    The first case is easy: The sequence tells me how long it is, and
    every
    element in it announces its length, so I can parse every element by
    looking at the next number of bytes as announced in each element.

    The second case is almost as easy: Just parse element by element,
    until we find a sequence delimitation item, which ends the sequence.

    It's the third case that worries me: The second item in Table 7.5-3
    has
    an unknown length, so I wouldn't know how many bytes to swallow
    until I can safely interpret the bytes making up the item delimitation
    tag and the sequence delimitation tag. In particular, the data of
    unknown length might, after all, include that byte sequence.

    How am I supposed to find out how far such an element of unknown
    value extends in the data stream?

    cheers,
    The Fish

  2. Re: Parsing SQ with unknown length containing data elements of unknownlength

    "The Fish" wrote:
    > I'm just trying to wrap my head around how to parse DICOM data. In
    > particular, I'm currently looking at PS 3.5-2008, Tables 7.5-{1,2,3}.

    [..]
    > It's the third case that worries me: The second item in Table 7.5-3
    > has
    > an unknown length, so I wouldn't know how many bytes to swallow
    > until I can safely interpret the bytes making up the item delimitation
    > tag and the sequence delimitation tag. In particular, the data of
    > unknown length might, after all, include that byte sequence.
    >
    > How am I supposed to find out how far such an element of unknown
    > value extends in the data stream?


    To make things worse, keep in mind that an item with undefined length
    may contain an attribute with Sequence VR of unknown length that contains
    items with unknown length and so on.

    Essentially you have to recursively parse through the item (i.e., read
    all attributes of that item) until you find the item delimitation element
    that belongs to this particular item. That delimiter will either be followed
    by another item tag (starting the next item of the sequence) or a sequence
    delimiter (ending the sequence).

    That said, in most cases you should just select a working implementation
    (DICOM toolkit) and just use that. This particular wheel has been re-invented
    often enough, unless you are targetting a very specific platform or
    programming language.

    Regards,
    Marco Eichelberg
    OFFIS

  3. Re: Parsing SQ with unknown length containing data elements ofunknown length

    Thanks for replying. Indeed, I find myself reinventing the wheel once
    more, since using a toolkit is not an option for me.

    On Oct 2, 1:53*pm, Marco Eichelberg
    wrote:
    > To make things worse, [...]


    In other words, there is no robust way of parsing an element with
    unspecified
    length?

  4. Re: Parsing SQ with unknown length containing data elements ofunknown length

    On Oct 7, 3:39 pm, doppelf...@gmail.com wrote:
    > Thanks for replying. Indeed, I find myself reinventing the wheel once
    > more, since using a toolkit is not an option for me.


    just out of curiosity, why not ?

    > On Oct 2, 1:53 pm, Marco Eichelberg
    > wrote:
    >
    > > To make things worse, [...]

    >
    > In other words, there is no robust way of parsing an element with
    > unspecified
    > length?


    you are just quoting a random part of the answer without the context !
    Of course it is *extremely* reliable to parse SQ and/or Item with
    undefined length. As Marco said, use a toolkit and you'll see that it
    is even more reliable than you think.

    DCMTK has an advanced feature where explicit length SQ in explicit
    length Item can be read, even when the last SQ has a corrupt length
    (AFAIK). But that should not happen normally. This also exhibit the
    fact that undefined length are so much nicer to handle (think implicit
    transfer syntax and private attribute).

    And you can even handle very long SQ (where size of SQ > UINT32_MAX)
    with undefined length, while you can't in explicit length.

    -Mathieu

  5. Re: Parsing SQ with unknown length containing data elements ofunknown length

    I recently wrote a DICOM reader in the Ruby programming language
    (http://dicom.rubyforge.org/), and I cant say that reading sequences/
    items with undefined length is that big a deal, the sequence/item
    delimitation tags will tell you when the sequences/items ends. You can
    have a look at the source code of some of the many open source readers
    that exist out there if you are still unsure how to solve it.

    Have you checked that there is not already a free/open DICOM reader
    for your particular programming language out there?

+ Reply to Thread