Unable to Compress DICOM Image File wtih Lossless JPEG

All other questions regarding DCMTK

Moderator: Moderator Team

Post Reply
Posts: 4
Joined: Thu, 2022-08-04, 15:08

Unable to Compress DICOM Image File wtih Lossless JPEG

#1 Post by shalas1 » Thu, 2022-08-04, 16:59


I have been using the DCMTK for the last couple months. Recently, I have been having trouble with compressing a DICOM image file with lossless JPEG. I have been following the code example at https://support.dcmtk.org/docs/mod_dcmjpeg.html, but I am still having problems. When calling the "chooseRepresentation" method of DcmDataset, I receive the OFCondition text "E: Unable to choose representation: Pixel representation cannot be changed". Below I have included the code snippet where I am seeing this issue. Note that I am calling the "newFileFormat.writeXML(std::cout)" method to show the contents of the dataset. The output to std::cout is also included below:

Code: Select all

        // Compress previously-created DICOM file with lossless JPEG and create new DICOM file
        // Register JPEG codecs
        DcmFileFormat newFileFormat;
        if (newFileFormat.loadFile(dcmFileNameOF).good()) {
            DcmDataset* newDataset = newFileFormat.getDataset();
            DcmItem* metaInfo = newFileFormat.getMetaInfo();
            DJ_RPLossless params;

            // Create lossless JPEG version of the dataset
            result = newDataset->chooseRepresentation(EXS_JPEGProcess14SV1, &params);
            if (result.bad()) {
                DCMNET_ERROR("Unable to choose representation: " << result.text());
            if (newDataset->canWriteXfer(EXS_JPEGProcess14SV1)) {
                // Store in lossless JPEG format
                qDebug() << "Creating lossless version!";
                newFileFormat.saveFile(testDcmFileNameOF, EXS_JPEGProcess14SV1);

"newFileFormat.writeXML(std::cout)" output:
<meta-header xfer="1.2.840.10008.1.2.1" name="Little Endian Explicit">
<element tag="0002,0000" vr="UL" vm="1" len="4" name="FileMetaInformationGroupLength">210</element>
<element tag="0002,0001" vr="OB" vm="1" len="2" name="FileMetaInformationVersion" binary="hidden"></element>
<element tag="0002,0002" vr="UI" vm="1" len="30" name="MediaStorageSOPClassUID">1.2.840.10008.</element>
<element tag="0002,0003" vr="UI" vm="1" len="60" name="MediaStorageSOPInstanceUID"></element>
<element tag="0002,0010" vr="UI" vm="1" len="22" name="TransferSyntaxUID">1.2.840.10008.</element>
<element tag="0002,0012" vr="UI" vm="1" len="28" name="ImplementationClassUID"></element>
<element tag="0002,0013" vr="SH" vm="1" len="16" name="ImplementationVersionName">OFFIS_DCMTK_367</element>
<data-set xfer="1.2.840.10008." name="JPEG Baseline">
<element tag="0008,0016" vr="UI" vm="1" len="30" name="SOPClassUID">1.2.840.10008.</element>
<element tag="0008,0018" vr="UI" vm="1" len="60" name="SOPInstanceUID"></element>
<element tag="0008,0020" vr="DA" vm="1" len="8" name="StudyDate">20220624</element>
E: Unable to choose representation: Pixel representation cannot be changed
Writing new file format contents:
<element tag="0008,0030" vr="TM" vm="1" len="4" name="StudyTime">5:48</element>
<element tag="0008,0050" vr="SH" vm="1" len="10" name="AccessionNumber">987654321</element>
<element tag="0008,0064" vr="CS" vm="1" len="4" name="ConversionType">WSD</element>
<element tag="0008,0080" vr="LO" vm="1" len="8" name="InstitutionName">HospTest</element>
<element tag="0008,0090" vr="PN" vm="1" len="8" name="ReferringPhysicianName">Dr. Test</element>
<element tag="0010,0010" vr="PN" vm="1" len="12" name="PatientName">PatientSteve</element>
<element tag="0010,0020" vr="LO" vm="1" len="10" name="PatientID">123456789</element>
<element tag="0010,0030" vr="DA" vm="1" len="10" name="PatientBirthDate">199610317</element>
<element tag="0010,0040" vr="CS" vm="1" len="2" name="PatientSex">M</element>
<element tag="0020,000d" vr="UI" vm="1" len="60" name="StudyInstanceUID"></element>
<element tag="0020,000e" vr="UI" vm="1" len="60" name="SeriesInstanceUID"></element>
<element tag="0020,0010" vr="SH" vm="1" len="6" name="StudyID">12346</element>
<element tag="0020,0011" vr="IS" vm="0" len="0" name="SeriesNumber"></element>
<element tag="0020,0013" vr="IS" vm="0" len="0" name="InstanceNumber"></element>
<element tag="0020,0020" vr="CS" vm="0" len="0" name="PatientOrientation"></element>
<element tag="0028,0002" vr="US" vm="1" len="2" name="SamplesPerPixel">3</element>
<element tag="0028,0004" vr="CS" vm="1" len="12" name="PhotometricInterpretation">YBR_FULL_422</element>
<element tag="0028,0006" vr="US" vm="1" len="2" name="PlanarConfiguration">0</element>
<element tag="0028,0010" vr="US" vm="1" len="2" name="Rows">130</element>
<element tag="0028,0011" vr="US" vm="1" len="2" name="Columns">195</element>
<element tag="0028,0100" vr="US" vm="1" len="2" name="BitsAllocated">8</element>
<element tag="0028,0101" vr="US" vm="1" len="2" name="BitsStored">8</element>
<element tag="0028,0102" vr="US" vm="1" len="2" name="HighBit">7</element>
<element tag="0028,0103" vr="US" vm="1" len="2" name="PixelRepresentation">0</element>
<sequence tag="7fe0,0010" vr="OB" card="2" name="PixelData">
<pixel-item len="0" binary="hidden"></pixel-item>
<pixel-item len="4020" binary="hidden"></pixel-item>

I'm not sure if there's something wrong with my DICOM file I'm extracting the dataset from. Does anyone have any suggestions?


Michael Onken
DCMTK Developer
Posts: 1898
Joined: Fri, 2004-11-05, 13:47
Location: Oldenburg, Germany

Re: Unable to Compress DICOM Image File wtih Lossless JPEG

#2 Post by Michael Onken » Fri, 2022-08-05, 08:57


the file is already compressed using JPEG baseline (see the Transfer Syntax 1.2.840.10008. = JPEG Baseline (Process 1).

You can only compress uncompressed pixel data (i.e. transfer syntaxes Little Endian Implicit/Explicit and Big Endian Explicit) using chooseRepresentation().

So one way is to decompress the dataset first. You can see in dcmcjpeg (the DCMTK tool for compressing DICOM images with JPEG) how this is done (mainly calling DJDecoderRegistration::registerCodecs() for JPEG decompression and then calling chooseRepresentation() with a uncompressed target transfer syntax; and after that, call chooseRepresentation() again as you do right now).

Best regards,

Posts: 4
Joined: Thu, 2022-08-04, 15:08

Re: Unable to Compress DICOM Image File wtih Lossless JPEG

#3 Post by shalas1 » Thu, 2022-08-11, 15:23

Thank you very much for the help. I have this all working now.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest