Write Dicom File to memory

All other questions regarding DCMTK

Moderator: Moderator Team

Post Reply
Message
Author
sic1981
Posts: 4
Joined: Tue, 2011-03-15, 17:50

Write Dicom File to memory

#1 Post by sic1981 »

Hi,

I try to write a dicom file into a memory buffer. I can write a file by using the DcmFileFormat::saveFile method. Now I have written a method DcmFileFormat::saveMem which uses a DcmOutputBufferStream to copy the Dicom file to an address in memory. It works so far but I have some problems to determine the exact size of the buffer to allocate for the dicom file. I tried the method DcmFileFormat::getLength( opt_oxfer, opt_oenctype) with the correct transfer syntax and encoding but the buffer is always about 100-200 bytes to small. How can I get the exact size of the dicom file in the memory?

Thanks in advance!

Uli Schlachter
DCMTK Developer
Posts: 120
Joined: Thu, 2009-11-26, 08:15

#2 Post by Uli Schlachter »

It looks like DcmFileFormat::getLength() only calculates the length of the dataset, so the metainfo header is ignored. Does DcmFileFormat::getMetaInfo()->calcElementLength() give you the missing number of bytes? This function should return at least 132 (DCM_PreambleLen + DCM_MagicLen), so this sounds like it's relatively close to your missing number of bytes.
There is also a getLength() method in DcmMetaInfo.
I don't know which of these would return the right value, sorry.

sic1981
Posts: 4
Joined: Tue, 2011-03-15, 17:50

#3 Post by sic1981 »

Thanks for the answer. But unfortunately that's not the solution :(

The value returned by DcmFileFormat::getLength() is equal to DcmDataset::getLength() + DcmFileFormat::getMetaInfo()->calcElementLength(). But there are still ~100Bytes missing.

Do you have any more ideas?

Jörg Riesmeier
ICSMED DICOM Services
ICSMED DICOM Services
Posts: 2217
Joined: Fri, 2004-10-29, 21:38
Location: Oldenburg, Germany

#4 Post by Jörg Riesmeier »

When the fileformat is written, some data elements in the meta information header are updated or added (if they were missing before) - see DcmFileFormat::validateMetaInfo(). So, I guess that the "missing bytes" are caused by this ...

Btw, why do you want to create a DICOM file in memory?

sic1981
Posts: 4
Joined: Tue, 2011-03-15, 17:50

#5 Post by sic1981 »

Hi,

okay I am almost there. If I first call DcmFileFormat::validateMetaInfo( opt_oxfer ) then DcmFileFormat::getLength( opt_oxfer, opt_oenctype ) return almost the exact length of the file. But there are still always 8 Bytes missing. If I add these 8 Bytes to the length all my test files seem to work. Do you have an idea what these 8 Bytes could be?

I want to write the file to shared memory to be able to access it by another process.

Jörg Riesmeier
ICSMED DICOM Services
ICSMED DICOM Services
Posts: 2217
Joined: Fri, 2004-10-29, 21:38
Location: Oldenburg, Germany

#6 Post by Jörg Riesmeier »

Do you have an idea what these 8 Bytes could be?
This depends on the content of the DICOM file, e.g. there was a fix only yesterday that solved an issue with wrong length calculation.
However, this issue only occurred when the VR of some data elements changed between reading and writing (e.g. from the invalid VR "??" to "UN").

Btw, what about using the DcmOutputBufferStream with a fixed buffer length, and then flush its content when the buffer is full?
There should be examples in the DCMTK source code on how to use this class that way ...

pkotre
Posts: 11
Joined: Tue, 2011-04-19, 14:09

#7 Post by pkotre »

Hi Jörg Riesmeier,

thank you very much for approach using DcmOutputBufferStream class with fixed buffer length.

It worked perfectly :)

Thanks and regards
Prakash Kotre

Post Reply

Who is online

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