Issue with Reading Pixel Data using getUncompressedFrame

All other questions regarding DCMTK

Moderator: Moderator Team

Post Reply
Message
Author
samee
Posts: 17
Joined: Thu, 2023-03-30, 15:27

Issue with Reading Pixel Data using getUncompressedFrame

#1 Post by samee »

Hi there,
I am trying to read pixel data of a frame directly to an array. I looked at some example code from diinpxt.h file but not sure where I am making the mistake. I have the following code:

Code: Select all

auto dcm_ff = DcmFileFormat();
auto dcm_tag_pixel_data = DcmTagKey(0x7fe0,0x0010) ;
dcm_ff.loadFile("/mnt/hdd8/axle/dev/dcmtk_reader/data/pydicom-data/data/eCT_Supplemental.dcm");
DcmPixelData pixel_data = DcmPixelData(dcm_tag_pixel_data);
DcmElement* pixel_data_ptr = &pixel_data;
dcm_ff.getDataset()->findAndGetElement(DCM_PixelData, pixel_data_ptr);
pixel_data.getUncompressedFrameSize(dcm_ff.getDataset(),frame_size);
frame_size % 2 == 0 ? frame_size = frame_size : frame_size = frame_size + 1; // make it even
std::cout << "Frame Size " << frame_size << std::endl;
auto buffer = new uint16_t[frame_size];
uint32_t start_fragment = 0;
OFString decompressed_color_model;
uint32_t frame_no = 0;
auto status = pixel_data.getUncompressedFrame(dcm_ff.getDataset(), 
                                    frame_no,  
                                    start_fragment, 
                                    OFreinterpret_cast(Uint16 *, buffer),
                                    frame_size,
                                    decompressed_color_model);

std::cout << status.text()<<std::endl;

I am unfortunately getting a Seg Fault inside DcmPixelData::getUncompressedFrame when the code calls DcmCodecList::decodeFrame. I never make in to the DcmCodecList::decodeFrame call. So, my suspicion is that the seg fault occurs when evaluating the arguments.
Below are some relevant tag data for the file I am working with.

Code: Select all

(0028, 0002) Samples per Pixel                   US: 1
(0028, 0004) Photometric Interpretation          CS: 'MONOCHROME2'
(0028, 0008) Number of Frames                    IS: '2'
(0028, 0010) Rows                                US: 512
(0028, 0011) Columns                             US: 512
(0028, 0100) Bits Allocated                      US: 16
(0028, 0101) Bits Stored                         US: 16
(0028, 0102) High Bit                            US: 15
(0028, 0103) Pixel Representation                US: 0
(0028, 0301) Burned In Annotation                CS: 'NO'
I would really appreciate if someone can point out where I am making the mistake.
Thanks!

Marco Eichelberg
OFFIS DICOM Team
OFFIS DICOM Team
Posts: 1437
Joined: Tue, 2004-11-02, 17:22
Location: Oldenburg, Germany
Contact:

Re: Issue with Reading Pixel Data using getUncompressedFrame

#2 Post by Marco Eichelberg »

Let's have a look:

You are creating a DcmPixelData instance called "pixel_data" on the stack (for no obvious reason):

Code: Select all

DcmPixelData pixel_data = DcmPixelData(dcm_tag_pixel_data);
Then you create a pointer that points to pixel_data:

Code: Select all

DcmElement* pixel_data_ptr = &pixel_data;
Then you run findAndGetElement():

Code: Select all

dcm_ff.getDataset()->findAndGetElement(DCM_PixelData, pixel_data_ptr);
You are not checking the return code of findAndGetElement(). If this method is successful, however, pixel_data_ptr now points to a different place, i.e. the PixelData element of the dataset, stored on the heap.

Then you call getUncompressedFrameSize() on the empty pixel data element on the stack, and pass a pointer to the dataset to which pixel_data does not belong. Guaranteed to fail.

Code: Select all

pixel_data.getUncompressedFrameSize(dcm_ff.getDataset(),frame_size);

Post Reply

Who is online

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