c++jpegdicomlosslessdcmtk

Compress DICOM file with DCMTK (C++)


damn i'm very frustated...

Following the example in this page http://support.dcmtk.org/docs/mod_dcmjpeg.html, I have written a C++ program to decompress a JPEG-compressed DICOM image file

Now I want to do the vice versa, from uncompressed to compressed and if I use the other example in the same page, with the same (or other file) the code compile and run but is not able to compress the file...

I saw that afetr the following code, the originale Xfer and the Current is the same, and this is not good because need to be different

dataset->chooseRepresentation(EXS_JPEGProcess14SV1, &params);

It's like the chooseRepresentation method fail....

More the line

dataset->canWriteXfer(EXS_JPEGProcess14SV1)

return false

I saw that in the dcpixel.cc file, with debugging the code go in

DcmPixelData::canChooseRepresentation(......... 
.... 
.... 
// representation not found, check if we have a codec that can create the 
// desired representation. 
if (original == repListEnd) 
   { 
     result = DcmCodecList::canChangeCoding(EXS_LittleEndianExplicit, toType.getXfer()); 
    } 

and result is FALSE....

How can I fix it? Someone have a code that works to compress a DICOM image with DCMTK or another library

This is the full code:

int main()
{

    //dcxfer.h
    DJDecoderRegistration::registerCodecs(); // register JPEG codecs
    DcmFileFormat fileformat;

    /**** MONO FILE ******/


    if (fileformat.loadFile("Files/cnv3DSlice (1)_cnv.dcm").good())
    {
        DcmDataset *dataset = fileformat.getDataset();
        DcmItem *metaInfo = fileformat.getMetaInfo();
        DJ_RPLossless params; // codec parameters, we use the defaults


        // this causes the lossless JPEG version of the dataset to be created
        dataset->chooseRepresentation(EXS_JPEGProcess14SV1, &params);

        // check if everything went well
        if (dataset->canWriteXfer(EXS_JPEGProcess14SV1))
        {
            // force the meta-header UIDs to be re-generated when storing the file
            // since the UIDs in the data set may have changed
            delete metaInfo->remove(DCM_MediaStorageSOPClassUID);
            delete metaInfo->remove(DCM_MediaStorageSOPInstanceUID);

            // store in lossless JPEG format
            fileformat.saveFile("Files/test_jpeg_compresso.dcm", EXS_JPEGProcess14SV1);
        }
    }

    DJDecoderRegistration::cleanup(); // deregister JPEG codecs

    return 0;
}

Solution

  • When trying to compress an image you need to call

    DJEncoderRegistration::registerCodecs();
    

    Decompress is

    DJDecoderRegistration::registerCodecs();