Invalid stream when trying to read from buffer

All other questions regarding DCMTK

Moderator: Moderator Team

Message
Author
martinrame
Posts: 347
Joined: Mon, 2009-02-23, 19:57

Invalid stream when trying to read from buffer

#1 Post by martinrame »

Hi, I need to read some tags from an image my program receives via network, as the files are really big (XA), I would like to load a buffer containing just the first 128kb for example, and look for the info I need.

I did some tests by writing my buffer to a file, and using "dcmdump +E -M file.dcm" and it was parsed as I expected.

I'm trying to read the dataset in a similar way as dcmdump does, but I'm getting "Invalid stream", here's the code:

Code: Select all

DcmInputBufferStream dataBuf;
dataBuf.setBuffer(buffer, buflen);
dataBuf.setEos();

DcmFileFormat fileFormat;
fileFormat.transferInit();
OFCondition cond = fileFormat.read(dataBuf, EXS_Unknown, EGL_noChange, buflen);
fileFormat.transferEnd();
cond.text(); // <--- Invalid stream
What I'm doing wrong?.

J. Riesmeier
DCMTK Developer
Posts: 2504
Joined: Tue, 2011-05-03, 14:38
Location: Oldenburg, Germany
Contact:

Re: Invalid stream when trying to read from buffer

#2 Post by J. Riesmeier »

So, you receive the DICOM dataset directly to file?

Would the "maxReadLength" parameter of DcmFileFormat::loadFile() help? By default, this method only loads element values that have a maximum length of 4 Kbytes (see "DCM_MaxReadLength").

martinrame
Posts: 347
Joined: Mon, 2009-02-23, 19:57

Re: Invalid stream when trying to read from buffer

#3 Post by martinrame »

J. Riesmeier wrote:So, you receive the DICOM dataset directly to file?

Would the "maxReadLength" parameter of DcmFileFormat::loadFile() help? By default, this method only loads element values that have a maximum length of 4 Kbytes (see "DCM_MaxReadLength").
Thanks for your answer Jörg. No, I cannot use loadFile, because I don't have a file, but a buffer containing just part of a dicom file.

I've tried using this:

Code: Select all

OFCondition cond = fileFormat.read(dataBuf, EXS_Unknown, EGL_noChange, 4096);
But I get the same error.

In another post you mentioned that my buffer (DcmInputBufferStream in this case) doesn't allow aleatory access (can't use seek or similar). Is this the case?, any workaround?.

J. Riesmeier
DCMTK Developer
Posts: 2504
Joined: Tue, 2011-05-03, 14:38
Location: Oldenburg, Germany
Contact:

Re: Invalid stream when trying to read from buffer

#4 Post by J. Riesmeier »

If you have no DICOM file why do you use DcmFileFormat at all (instead of DcmDataset)?

martinrame
Posts: 347
Joined: Mon, 2009-02-23, 19:57

Re: Invalid stream when trying to read from buffer

#5 Post by martinrame »

J. Riesmeier wrote:If you have no DICOM file why do you use DcmFileFormat at all (instead of DcmDataset)?
Good question.

I modified my code to use a DcmDataset instead of DcmFileFormat:

Code: Select all

  DcmInputBufferStream dataBuf;
  dataBuf.setBuffer(buffer, buflen);
  dataBuf.setEos();

  DcmDataset dset;
  OFCondition cond = dset.read(dataBuf, EXS_Unknown, EGL_noChange, 2048);
  cond.text();  
But I'm getting the same "Invalid stream" message. Any hint?.

martinrame
Posts: 347
Joined: Mon, 2009-02-23, 19:57

Re: Invalid stream when trying to read from buffer

#6 Post by martinrame »

I did some more tests using DcmItem and DcmMetaInfo, instead of DcmDataset. None of them stopped with an "Invalid stream" error, but they (of course) don't get the information I need.

Here are the results:

DcmMetaInfo:

Code: Select all

# Dicom-Meta-Information-Header
# Used TransferSyntax: Little Endian Explicit
(0002,0000) UL 188                                      #   4, 1 FileMetaInformationGroupLength
(0002,0001) OB 00\01                                    #   2, 1 FileMetaInformationVersion
(0002,0002) UI =SecondaryCaptureImageStorage            #  26, 1 MediaStorageSOPClassUID
(0002,0003) UI [G08008453.20140607124845.1]             #  26, 1 MediaStorageSOPInstanceUID
(0002,0010) UI =JPEGLossless:Non-hierarchical-1stOrderPrediction #  22, 1 TransferSyntaxUID
(0002,0012) UI [1.2.276.0.7230010.3.0.3.6.0]            #  28, 1 ImplementationClassUID
(0002,0013) SH [OFFIS_DCMTK_360]                        #  16, 1 ImplementationVersionName
(0002,0016) AE [PACSLINK]                               #   8, 1 SourceApplicationEntityTitle
DcmItem:

Code: Select all

(fffe,e000) na (Item with explicit length #=0)          #   0, 1 Item
(fffe,e00d) na (ItemDelimitationItem for re-encoding)   #   0, 0 ItemDelimitationItem

J. Riesmeier
DCMTK Developer
Posts: 2504
Joined: Tue, 2011-05-03, 14:38
Location: Oldenburg, Germany
Contact:

Re: Invalid stream when trying to read from buffer

#7 Post by J. Riesmeier »

The information in the meta-header indicate that your buffer actually contains the byte stream of a DICOM file...

Where is the buffer content coming from?

martinrame
Posts: 347
Joined: Mon, 2009-02-23, 19:57

Re: Invalid stream when trying to read from buffer

#8 Post by martinrame »

J. Riesmeier wrote:The information in the meta-header indicate that your buffer actually contains the byte stream of a DICOM file...

Where is the buffer content coming from?
I have a client app that requests an image stored on a http server that serve requests using a CGI app. When part of the data comes to the client, I need to start parsing the header of it, before loading the whole file (because this can take too long for large files like Angio images XA).

J. Riesmeier
DCMTK Developer
Posts: 2504
Joined: Tue, 2011-05-03, 14:38
Location: Oldenburg, Germany
Contact:

Re: Invalid stream when trying to read from buffer

#9 Post by J. Riesmeier »

OK, I understand. If it's really a DICOM file (with meta-header) then DcmFileFormat is the correct class.

Btw, I tried dcmdump with a DICOM XA file cut off after 128K (using "dd") and it works when I use option --ignore-errors (+E) or -ignore-parse-errors (+Ep). So, the input buffer stream approach should work in the same manner (though I haven't tried it). The error message for the dcmdump example (without the ignore error options) was:

Code: Select all

E: dcmdump: I/O suspension or premature end of stream: reading file: test.dcm

martinrame
Posts: 347
Joined: Mon, 2009-02-23, 19:57

Re: Invalid stream when trying to read from buffer

#10 Post by martinrame »

Can you load that 128k file into a (void *) buffer, then try to open with DcmFileFormat?, then Print it's header (at least the part that can be read)?. That's exactly what I'm trying to do.

J. Riesmeier
DCMTK Developer
Posts: 2504
Joined: Tue, 2011-05-03, 14:38
Location: Oldenburg, Germany
Contact:

Re: Invalid stream when trying to read from buffer

#11 Post by J. Riesmeier »

I tested it, and it works in the same manner.

I don't know whether it makes any difference, but I used the current development version of the DCMTK.

martinrame
Posts: 347
Joined: Mon, 2009-02-23, 19:57

Re: Invalid stream when trying to read from buffer

#12 Post by martinrame »

Jörg, here's a small program to show what I'm trying to do:

Code: Select all

#include <dcmtk/config/osconfig.h>
#define INCLUDE_CSTDLIB
#define INCLUDE_CSTDIO
#define INCLUDE_CSTRING
#include "dcmtk/ofstd/ofstdinc.h"
#include "dcmtk/dcmdata/dcistrmb.h"
#include "dcmtk/dcmdata/dcdict.h"
#include <dcmtk/dcmdata/dcfilefo.h>

using namespace std;

int main(int argc, char *argv[])
{
  FILE *f;
  int const MAX_FILE_SIZE = 8192;
  void *  buffer[MAX_FILE_SIZE];
  int buflen;

  if(argc == 1) {
    cerr << "Please provide a file." << endl;
    return 0;
  };

  f = fopen(argv[1], "rb");
  if (f)
  {
      buflen = fread(buffer, MAX_FILE_SIZE, 1, f);
      DcmInputBufferStream dataBuf;
      dataBuf.setBuffer(buffer, buflen);
      dataBuf.setEos();

      DcmFileFormat fileFormat;
      fileFormat.transferInit();
      OFCondition cond = fileFormat.read(dataBuf, EXS_Unknown, EGL_noChange, buflen);
      fileFormat.transferEnd();

      cout << "Result: " << cond.text() << endl;
      cout << "Dump: " << endl;
      fileFormat.print(cout);
  }
  else
  {
      cout << "Error opening file " << argv[1] << endl;
  }
}
I truncated an XA file to 128k using this command:

Code: Select all

truncate -s KB 128 file.dcm
This is the output of ./dumpbuffer file.dcm:

Code: Select all

Result: End of stream
Dump: 

# Dicom-File-Format

# Dicom-Meta-Information-Header
# Used TransferSyntax: Little Endian Explicit

# Dicom-Data-Set
# Used TransferSyntax: Unknown Transfer Syntax
Do you know why there's no dicom tag shown?.

P.s.: Compiled against 3.6.0.

J. Riesmeier
DCMTK Developer
Posts: 2504
Joined: Tue, 2011-05-03, 14:38
Location: Oldenburg, Germany
Contact:

Re: Invalid stream when trying to read from buffer

#13 Post by J. Riesmeier »

I haven't compiled your code, but guess that the error is in this line:

Code: Select all

buflen = fread(buffer, MAX_FILE_SIZE, 1, f);
You read 1 element of data, which has a size of 8192 bytes. So, fread() returns 1 (for the number of read elements)...

martinrame
Posts: 347
Joined: Mon, 2009-02-23, 19:57

Re: Invalid stream when trying to read from buffer

#14 Post by martinrame »

I just want to read 8192 bytes, Is my code correct?.

J. Riesmeier
DCMTK Developer
Posts: 2504
Joined: Tue, 2011-05-03, 14:38
Location: Oldenburg, Germany
Contact:

Re: Invalid stream when trying to read from buffer

#15 Post by J. Riesmeier »

As far as I can see, NO. Reading the man page of fread() you'll notice that you have to swap the second and third parameter.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest