Calculate the buffer size before serialize DICOM to memory

All other questions regarding DCMTK

Moderator: Moderator Team

Post Reply
Message
Author
sfzhang
Posts: 10
Joined: Thu, 2014-08-28, 08:30
Location: Shanghai, China

Calculate the buffer size before serialize DICOM to memory

#1 Post by sfzhang »

In the post Write DICOM file to memory, @J. Riesmeier sayed the buffer size can be got by calling DcmDataset::calcElementLength() . And I try the following code:

Code: Select all

// Get the buffer length
Uint32 meta_length = m_dcm_file_format->getMetaInfo()->calcElementLength(EXS_Unknown, EET_UndefinedLength);
Uint32 data_length = m_dcm_file_format->getDataset()->calcElementLength(EXS_Unknown, EET_UndefinedLength);

// Serialize to buffer which size bigger enough
DcmOutputBufferStream dcm_output_stream(buffer, size);
m_dcm_file_format->transferInit();
OFCondition ret = m_dcm_file_format->write(dcm_output_stream, EXS_Unknown, EET_UndefinedLength, NULL);
m_dcm_file_format->transferEnd();

if (!ret.good()) {
    return false;
}   

// Get the length after serialization
offile_off_t s = 0;
dcm_output_stream.flushBuffer(buffer, s);

assert(s == (meta_length + data_length)); // Assert failed!!
Why is that the size returned by metaInfo->calcElementLength()+dataset->calcElementLength() is NOT equal to size got from flushBuffer() after serialization?
And I tested several DICOM images and found s - (meta_length + data_length) always seems to be equal to 40.

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

Re: Calculate the buffer size before serialize DICOM to memo

#2 Post by J. Riesmeier »

I haven't checked but I guess that the difference results from the file meta information header (not from the dataset). If this is the case, it is a consequence of the fact that the write() method adds missing data if needed (e.g. SOP Instance UID and SOP Class UID).

sfzhang
Posts: 10
Joined: Thu, 2014-08-28, 08:30
Location: Shanghai, China

Re: Calculate the buffer size before serialize DICOM to memo

#3 Post by sfzhang »

I tested the code using GE MRI image which can be downloaded from here.
And I found out the extra 8 bytes in 40 bytes may come from ImplementationClassUID(0002,0012) tag:
Before serialize to buffer:
(0002,0000) FileMetaInformationGroupLength UL 1 4 190
(0002,0012) ImplementationClassUID UI 1 20 1.2.840.113619.6.244
(0002,0013) ImplementationVersionName SH 1 16 90701_293b_HDMR2
After serialize to buffer:
(0002,0000) FileMetaInformationGroupLength UL 1 4 198
(0002,0012) ImplementationClassUID UI 1 28 1.2.276.0.7230010.3.0.3.6.1
(0002,0013) ImplementationVersionName SH 1 16 OFFIS_DCMTK_361

I cannot find out the difference about the other 32 bytes.

Post Reply

Who is online

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