Loading DICOM file from memory buffer

All other questions regarding DCMTK

Moderator: Moderator Team

Post Reply
Message
Author
artemmikheev
Posts: 32
Joined: Mon, 2018-11-12, 20:01

Loading DICOM file from memory buffer

#1 Post by artemmikheev »

I can use DcmFileFormat::loadFile function with specifying file to load by name.

But my application has the DICOM files stored in separate memory buffers
f.e. BYTE* dcmFileInMemory (int size is known). Is there a way to load it with the existing
library without saving it to a new file and the using DcmFileFormat::loadFile?
I understand that solution might be in C++ and not strictly through DCMTK.
thanks

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

Re: Loading DICOM file from memory buffer

#2 Post by J. Riesmeier »

The following code fragment (from "dcmdata/tests/dctmacro.h") shows how this works in principle:

Code: Select all

/**
 * Read the dataset from the buffer into a dataset object.
 * @param dset the dataset to read into
 * @param buffer the buffer to read from
 * @param length the length of the buffer
 * @param ts the transfer syntax that is used
 * @return EC_Normal if reading worked, else an error
 */
static inline OFCondition readDataset(DcmDataset& dset, const Uint8* buffer, size_t length, E_TransferSyntax ts)
{
    DcmInputBufferStream stream;
    stream.setBuffer(buffer, length);
    stream.setEos();

    dset.clear();
    dset.transferInit();
    const OFCondition cond = dset.read(stream, ts);
    dset.transferEnd();

    return cond;
}

artemmikheev
Posts: 32
Joined: Mon, 2018-11-12, 20:01

Re: Loading DICOM file from memory buffer

#3 Post by artemmikheev »

Thank you so much - will try immediately!

artemmikheev
Posts: 32
Joined: Mon, 2018-11-12, 20:01

Re: Loading DICOM file from memory buffer

#4 Post by artemmikheev »

Thanks again for suggestion. Unfortunately it did not work. After reading several items it was returning "Invalid Stream"
But I was able to re-write the following function, so instead it take the Buffer/BufferSize and it works fine now.


virtual OFCondition loadFileUntilTag(const OFFilename &fileName,
const E_TransferSyntax readXfer = EXS_Unknown,
const E_GrpLenEncoding groupLength = EGL_noChange,
const Uint32 maxReadLength = DCM_MaxReadLength,
const E_FileReadMode readMode = ERM_autoDetect,
const DcmTagKey &stopParsingAtElement = DCM_UndefinedTagKey);

So thank you, the key was the usage of DcmInputBufferStream class, as you suggested.

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

Re: Loading DICOM file from memory buffer

#5 Post by J. Riesmeier »

Of course, a DICOM file cannot be read by an instance of the DcmDataset class. This is what DcmFileFormat is for ;-)

Post Reply

Who is online

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