title: DICOM Accessing Compressed Data (DCMTK)
body: <p>I'm struggling with the DCMTK 3.6.1 library and now I'm blocked in a common situation: accessing compress data pixel of a DICOM image.</p>
  
  <p>As the authors of the library suggest here <a href="http://support.dcmtk.org/redmine/projects/dcmtk/wiki/Howto_AccessingCompressedData" rel="nofollow">http://support.dcmtk.org/redmine/projects/dcmtk/wiki/Howto_AccessingCompressedData</a>, this is the correct way to get the compress data.</p>
  
  <p>This code work, but they start from a file to create the dataset. 
  In my code I have my dataset filled in this way</p>
  
  <pre><code>status = data-&gt;putAndInsertUint8Array(DCM_PixelData, pSource, sizeBuffer);
  </code></pre>
  
  <p>where pSource contains my UNcompressed pixel data. After this I add the Image DICOM tags (Group 28) and make the compression with </p>
  
  <pre><code>status = data-&gt;chooseRepresentation(EXS_JPEGProcess14SV1, &amp;param);
  </code></pre>
  
  <p>and starting from this dataset I want to acces to compressed data</p>
  
  <pre><code>status = data-&gt;findAndGetElement(DCM_PixelData, element);
      DcmPixelData *dpix = NULL;
      dpix = OFstatic_cast(DcmPixelData*, element);
      /* Since we have compressed data, we must utilize DcmPixelSequence
      in order to access it in raw format, e. g. for decompressing it
      with an external library.
      */
      DcmPixelSequence *dseq = NULL;
      E_TransferSyntax xferSyntax = EXS_Unknown;
      const DcmRepresentationParameter *rep = NULL;
      // Find the key that is needed to access the right representation of the data within DCMTK
      dpix-&gt;getOriginalRepresentationKey(xferSyntax, rep);
  
      // Access original data representation and get result within pixel sequence
      status = dpix-&gt;getEncapsulatedRepresentation(xferSyntax, rep, dseq);
      Uint32 length;
      if (status.good())
      {
          DcmPixelItem* pixitem = NULL;
          // Access first frame (skipping offset table)
          dseq-&gt;getItem(pixitem, 1);
          if (pixitem == NULL)
              return 1;
          Uint8* pixData = NULL;
          // Get the length of this pixel item (i.e. fragment, i.e. most of the time, the lenght of the frame)
          length = pixitem-&gt;getLength();
          if (length == 0)
              return 1;
          // Finally, get the compressed data for this pixel item
          status = pixitem-&gt;getUint8Array(pixData);
          // Metto i Pixel Data compressi su pSorgCompr
          pSorgCompr = (LPBYTE)pixData;
      }
      ////////////////////////////
  
      DJEncoderRegistration::cleanup();
      DJDecoderRegistration::cleanup();
  </code></pre>
  
  <p>but the row <em>status = dpix->getEncapsulatedRepresentation(xferSyntax, rep, dseq);</em> retun a FAIL with error "Pixel Representation not found" and I don't understand why.</p>
  
  <p>The <strong>strage thing</strong> is that if before accesing the compressed data, I save the compressed file with <em>fileformat.saveFile("compressedPixelData.dcm", EXS_JPEGProcess14SV1);</em> and next I load the file with <em>result = fileformat.loadFile("compressedPixelData.dcm");</em>, <strong>all works perfectly</strong></p>
  
  <p>It's like the loadfile function solve the problem, I do not know how, maybe filling some tags?</p>
  
  <p>The tags that I fill before calling the chooseRepresentation function are:</p>
  
  <ul>
  <li>DCM_Rows</li>
  <li>DCM_Columns</li>
  <li>DCM_BitsStored</li>
  <li>DCM_SamplesPerPixel </li>
  <li>DCM_PlanarConfiguration</li>
  <li>DCM_HighBit</li>
  <li>DCM_BitsAllocated</li>
  <li>DCM_PixelRepresentation</li>
  <li>DCM_RescaleIntercept</li>
  <li>DCM_RescaleSlope</li>
  <li>DCM_PhotometricInterpretation</li>
  <li>DCM_PixelAspectRatio</li>
  <li>DCM_ImagerPixelSpacing</li>
  <li>DCM_PixelSpacing</li>
  </ul>