Decompress a JPEG DICOM image starting from PixData

All other questions regarding DCMTK

Moderator: Moderator Team

Message
Author
GiordiX
Posts: 27
Joined: Fri, 2015-01-23, 15:36

Decompress a JPEG DICOM image starting from PixData

#1 Post by GiordiX » Tue, 2015-02-10, 15:12

Hi to all,
I want to decompress a DICOM file like in this example http://support.dcmtk.org/docs/mod_dcmjpeg.html
But my problem is that I don't want to load a file.

I have an array which inside there are the PixelData values Compressed. I have tried in this way but doesn't work.

Code: Select all

DJDecoderRegistration::registerCodecs();
DJEncoderRegistration::registerCodecs();

DcmFileFormat fileformat;
DJ_RPLossless param_lossless;
		
DcmDataset *pDataset = fileformat.getDataset();
pDataset->chooseRepresentation(EXS_JPEGProcess14SV1, &param_lossless);
				
BYTE*	pBufferImg = (BYTE*)pArray->ImgDicom.GetAt(0); //here I have my PixelData
//I put it into my dataset
pDataset->putAndInsertUint8Array(DCM_PixelData, pBufferImg, pArray->NumByteImg);
//decompress
OFCondition status = pDataset->chooseRepresentation(EXS_LittleEndianExplicit, NULL); //status is OK
...
//add all the tags like Rows, Columuns, BitStored, etc
...
if (pDataset->canWriteXfer(EXS_LittleEndianExplicit))
{
	fileformat.saveFile("test.dcm", EXS_LittleEndianExplicit);
}

DJDecoderRegistration::cleanup(); // deregister JPEG codecs
DJEncoderRegistration::cleanup();
The file test.dcm is created but I can't open it (free Dicom Viewr software crash) and the dimension is equal to the compressed file, so the decode procedure doesn't work... What is my error?

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

Re: Decompress a JPEG DICOM image starting from PixData

#2 Post by J. Riesmeier » Tue, 2015-02-10, 19:21

According to the DICOM standard, compressed pixel data is stored in a special way, so-called "encapsulated format" (see DICOM part 5 for details). So, your "simple" approach with the byte array does not work.

GiordiX
Posts: 27
Joined: Fri, 2015-01-23, 15:36

Re: Decompress a JPEG DICOM image starting from PixData

#3 Post by GiordiX » Wed, 2015-02-11, 09:14

J. Riesmeier wrote:According to the DICOM standard, compressed pixel data is stored in a special way, so-called "encapsulated format" (see DICOM part 5 for details). So, your "simple" approach with the byte array does not work.
Oh I see, so what I can do with DCMTK library? I have also tried in this way

Code: Select all

pDataset->findAndGetElement(DCM_PixelData, dummyElem);
						
Uint32 frameSize;
dummyElem->getUncompressedFrameSize(pDataset, frameSize);

BYTE* buf = new BYTE[frameSize];
OFString decompressedColorModel;
Uint32 startFragment = 0;
Uint32 fakeSize = frameSize - 1;

pDataset->loadAllDataIntoMemory();
DcmFileCache *cache = NULL;

OFCondition status = dummyElem->getUncompressedFrame(pDataset, 0, startFragment, buf, fakeSize, decompressedColorModel, cache);
If I use frameSize instead of frameSize-1 I have "Too many bytes requested" this is why I have used minus one.

The Frame size is correct but getUncompressedFrame is Illegal call, perhaps wrong parameter and I don't understand why....
Last edited by GiordiX on Wed, 2015-02-11, 09:27, edited 1 time in total.

GiordiX
Posts: 27
Joined: Fri, 2015-01-23, 15:36

Re: Decompress a JPEG DICOM image starting from PixData

#4 Post by GiordiX » Wed, 2015-02-11, 09:16

This is another way that I have found as a solution to my problem on internet

Code: Select all

///. get PixelData element in DCM dataset
pDcmDataSet->findAndGetPixelData(...);
///. get PixelSequence in PixelData element
pPixelData->getEncapsulatedRepresentation(...)
///. get PixelItem in PixelSequence
pDcmPixelSequence->getItem(...);
///. get frame in Pixel Item
pPixelItem->getUint8Arrary(...);
But in my case the getItem fail...
Can you provide some example that use the array of the Pixel Data?

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

Re: Decompress a JPEG DICOM image starting from PixData

#5 Post by J. Riesmeier » Wed, 2015-02-11, 11:39

Unfortunately, it's not clear to me what "pArray->ImgDicom" contains. Is it a JPEG stream (just the pixel data with JPEG markers) or the content of a DICOM Pixel Data element (where the transfer syntax of the DICOM dataset is JPEG lossless compressed).

GiordiX
Posts: 27
Joined: Fri, 2015-01-23, 15:36

Re: Decompress a JPEG DICOM image starting from PixData

#6 Post by GiordiX » Wed, 2015-02-11, 12:16

J. Riesmeier wrote:Unfortunately, it's not clear to me what "pArray->ImgDicom" contains. Is it a JPEG stream (just the pixel data with JPEG markers) or the content of a DICOM Pixel Data element (where the transfer syntax of the DICOM dataset is JPEG lossless compressed).
The second, a DICOM Pixel Data element (where the transfer syntax of the DICOM dataset is JPEG lossless compressed).

My approach was move this PixelData in one dataset, change the representation, and save the file but as you say this is not possibile.

So I change my approach: move this PixelData in one dataset, get again the PixelData and put it in one dummy DcmElement, get the FrameSize from the dummy (and I have the value that I expected for the decomressed file to save), get the Frame with dummyElem->getUncompressedFrame but I'm stuck in this part, where I have "Illegal call perhaps wrong parameter" error and I can't understand why....

The codec are registered...
The size is correct... Where is my error?

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

Re: Decompress a JPEG DICOM image starting from PixData

#7 Post by J. Riesmeier » Wed, 2015-02-11, 14:12

What do you mean by "move this PixelData in one dataset"? How did you actually do that?

GiordiX
Posts: 27
Joined: Fri, 2015-01-23, 15:36

Re: Decompress a JPEG DICOM image starting from PixData

#8 Post by GiordiX » Wed, 2015-02-11, 14:20

J. Riesmeier wrote:What do you mean by "move this PixelData in one dataset"? How did you actually do that?
With a putAndInsert function

Code: Select all

dataset->putAndInsertUint8Array(DCM_PixelData, bufferOfMyPixelDataCompressed, NumByteImg); 
But in any case, if you for a second forget my code, if I have an array which inside there is the PixelData value compressed (coming from a compressed Dicom file), how can I generate and save a DICOM file uncompressed? A decode request, but without loading any file...

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

Re: Decompress a JPEG DICOM image starting from PixData

#9 Post by J. Riesmeier » Wed, 2015-02-11, 14:28

I'm sorry but this line does not show how you "move pixel data" (it only puts a byte array into the pixel data element, which only works for uncompressed pixel data, of course). Could you please provide a more detailed description on what you actually want to do. As I said, compressed pixel data is stored differently ("encapsulated format").

GiordiX
Posts: 27
Joined: Fri, 2015-01-23, 15:36

Re: Decompress a JPEG DICOM image starting from PixData

#10 Post by GiordiX » Wed, 2015-02-11, 14:44

J. Riesmeier wrote:I'm sorry but this line does not show how you "move pixel data" (it only puts a byte array into the pixel data element, which only works for uncompressed pixel data, of course). Could you please provide a more detailed description on what you actually want to do. As I said, compressed pixel data is stored differently ("encapsulated format").
First of all thanks for your answers.
I want to do the same thing that works for uncompressed pixel data but with compressed pixel data.

So, inside an array I have compressed pixel data and starting from this I want another byte array that contains the UNcompressed Pixel data; in such a way now I can use the putAndInsertUint8Array function because, as you said, this "only works for uncompressed pixel data".

Hope is clear, sorry for my english

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

Re: Decompress a JPEG DICOM image starting from PixData

#11 Post by J. Riesmeier » Wed, 2015-02-11, 15:05

So, inside an array I have compressed pixel data and starting from this I want another byte array that contains the UNcompressed Pixel data; in such a way now I can use the putAndInsertUint8Array function because, as you said, this "only works for uncompressed pixel data".
I think we go i circles. I still don't know what your use case is. You have a byte array with JPEG compressed data and want to generate a decompressed version of the contained pixel data, right? So, where does DICOM come into play?

GiordiX
Posts: 27
Joined: Fri, 2015-01-23, 15:36

Re: Decompress a JPEG DICOM image starting from PixData

#12 Post by GiordiX » Wed, 2015-02-11, 15:19

J. Riesmeier wrote:
So, inside an array I have compressed pixel data and starting from this I want another byte array that contains the UNcompressed Pixel data; in such a way now I can use the putAndInsertUint8Array function because, as you said, this "only works for uncompressed pixel data".
I think we go i circles. I still don't know what your use case is. You have a byte array with JPEG compressed data and want to generate a decompressed version of the contained pixel data, right? So, where does DICOM come into play?
Yes, right.

Because for make the compression of the data (the same that now I want uncompressed) I have used the DCMTK library in this way:

starting from an array of byte of my Uncompressed Jpeg Image, I put it into a dataset with
dataset->putAndInsertUint8Array(DCM_PixelData, myArraySource, sizeBuffer)
//add all the necessary DICOM tags Rows, Columns, Bitstored, etc
//make the compression with
pdataset->chooseRepresentation(EXS_JPEGProcess14SV1, &param_lossless);
//accessing to compressed pixel data with the procedure of the HowTo (http://support.dcmtk.org/redmine/projec ... ressedData)
//now I have the original compressed pixel data

What I want to do now is the viceversa, starting from the original compressed pixel data, I want to uncompressed it and save in a new array, that I can transfer in a dataset using putAndInsertUint8Array. How can I do this???
I must work with the array because I do this things inside another existing project that use the DICOM files.

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

Re: Decompress a JPEG DICOM image starting from PixData

#13 Post by J. Riesmeier » Wed, 2015-02-11, 18:50

You have to create a DcmPixelSequence, insert the pixel items with the JPEG data (the first item always contains the basic offset table), and then use DcmPixelData::putOriginalRepresentation() to put the pixel sequence to the DICOM PixelData element. Code examples can be found both in "dump2dcm.cc" and in "xml2dcm.cc". Also "img2dcm" and its underlying classes should contain such code, e.g. in Image2Dcm::insertEncapsulatedPixelData().

But before you do that, please read the relevant parts of DICOM P3.5 and make sure that you understood how the encapsulated format for compressed pixel data works.

GiordiX
Posts: 27
Joined: Fri, 2015-01-23, 15:36

Re: Decompress a JPEG DICOM image starting from PixData

#14 Post by GiordiX » Thu, 2015-02-12, 17:34

J. Riesmeier wrote:You have to create a DcmPixelSequence, insert the pixel items with the JPEG data (the first item always contains the basic offset table), and then use DcmPixelData::putOriginalRepresentation() to put the pixel sequence to the DICOM PixelData element. Code examples can be found both in "dump2dcm.cc" and in "xml2dcm.cc". Also "img2dcm" and its underlying classes should contain such code, e.g. in Image2Dcm::insertEncapsulatedPixelData().
But before you do that, please read the relevant parts of DICOM P3.5 and make sure that you understood how the encapsulated format for compressed pixel data works.
SOLVED! Thank you very much for your advices. :wink:
insertEncapsulatedPixelData() is exactly what I need: I made the same steps of this function and I have put my pixel data in my dataset in the correct way.
I have also read the relevant parts of DICOM P3.5 and I have understood some part of the encapsulated format, but not all.
It's quite difficult for me but I will study more.

jdinca
Posts: 9
Joined: Thu, 2012-10-25, 07:18

Re: Decompress a JPEG DICOM image starting from PixData

#15 Post by jdinca » Tue, 2015-09-08, 21:37

GiordiX wrote:
J. Riesmeier wrote:You have to create a DcmPixelSequence, insert the pixel items with the JPEG data (the first item always contains the basic offset table), and then use DcmPixelData::putOriginalRepresentation() to put the pixel sequence to the DICOM PixelData element. Code examples can be found both in "dump2dcm.cc" and in "xml2dcm.cc". Also "img2dcm" and its underlying classes should contain such code, e.g. in Image2Dcm::insertEncapsulatedPixelData().
But before you do that, please read the relevant parts of DICOM P3.5 and make sure that you understood how the encapsulated format for compressed pixel data works.
SOLVED! Thank you very much for your advices. :wink:
insertEncapsulatedPixelData() is exactly what I need: I made the same steps of this function and I have put my pixel data in my dataset in the correct way.
I have also read the relevant parts of DICOM P3.5 and I have understood some part of the encapsulated format, but not all.
It's quite difficult for me but I will study more.
Would it be possible to elaborate on how you use the function insertEncapsulatedPixelData? It's a private method, and I'm wondering how you exposed it. I too would like to convert a compressed stream using the DCMTK jpeg decoder. Thanks,

Post Reply

Who is online

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