title: How create a DICOM image from byte (DCMTK)
body: <p>I want to use the DCMTK 3.6.1 library in an existing project that can create DICOM image. I want to use this library because I want to make the compression of the DICOM images. In a new solution (Visual Studio 2013/C++) Following the example in the DCMTK official documentation, I have this code, that works properly.</p>
  
  <pre><code>using namespace std;
  
  int main()
  {
  
      DJEncoderRegistration::registerCodecs();
      DcmFileFormat fileformat;
  
      /**** MONO FILE ******/
  
      if (fileformat.loadFile("Files/test.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 EXS_JPEGProcess14SV1
          dataset-&gt;chooseRepresentation(EXS_JPEGProcess14SV1, &amp;params);   
  
          // check if everything went well
          if (dataset-&gt;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-&gt;remove(DCM_MediaStorageSOPClassUID);
              delete metaInfo-&gt;remove(DCM_MediaStorageSOPInstanceUID);
  
              metaInfo-&gt;putAndInsertString(DCM_ImplementationVersionName, "New Implementation Version Name");
              //delete metaInfo-&gt;remove(DCM_ImplementationVersionName);
              //dataset-&gt;remove(DCM_ImplementationVersionName);
  
              // store in lossless JPEG format
              fileformat.saveFile("Files/carrellata_esami_compresso.dcm", EXS_JPEGProcess14SV1);
          }
      }
  
      DJEncoderRegistration::cleanup();
  
      return 0;
  }
  </code></pre>
  
  <p>Now I want to use the same code in an existing C++ application where </p>
  
  <pre><code>if (infoDicom.arrayImgDicom.GetSize() != 0)  //Things of existing previous code
        {
  
       //I have added here the registration
       DJEncoderRegistration::registerCodecs(); // register JPEG codecs
  
       DcmFileFormat fileformat;
       DcmDataset *dataset = fileformat.getDataset();
       DJ_RPLossless params;
  
        dataset-&gt;putAndInsertUint16(DCM_Rows, infoDicom.rows);
       dataset-&gt;putAndInsertUint16(DCM_Columns, infoDicom.columns,);
       dataset-&gt;putAndInsertUint16(DCM_BitsStored, infoDicom.m_bitstor);
       dataset-&gt;putAndInsertUint16(DCM_HighBit, infoDicom.highbit);
       dataset-&gt;putAndInsertUint16(DCM_PixelRepresentation, infoDicom.pixelrapresentation);
       dataset-&gt;putAndInsertUint16(DCM_RescaleIntercept, infoDicom.rescaleintercept);
       dataset-&gt;putAndInsertString(DCM_PhotometricInterpretation,"MONOCHROME2");
       dataset-&gt;putAndInsertString(DCM_PixelSpacing, "0.086\\0.086");  
       dataset-&gt;putAndInsertString(DCM_ImagerPixelSpacing, "0.096\\0.096");
       BYTE* pData = new BYTE[sizeBuffer];
  
       LPBYTE   pSorg;
  
       for (int nf=0; nf&lt;iNumberFrames; nf++)
       {
          //this contains all the PixelData and I put it into the dataset
          pSorg = (BYTE*)infoDicom.arrayImgDicom.GetAt(nf);
          dataset-&gt;putAndInsertUint8Array(DCM_PixelData, pSorg, sizeBuffer);
  
          dataset-&gt;chooseRepresentation(EXS_JPEGProcess14SV1, &amp;params);
          //and I put it in my data set            
  
  
           //but this IF return false so che canWriteXfer fails...
          if (dataset-&gt;canWriteXfer(EXS_JPEGProcess14SV1))
          {
             dataset-&gt;remove(DCM_MediaStorageSOPClassUID);
             dataset-&gt;remove(DCM_MediaStorageSOPInstanceUID);
          }
  
              //the saveFile fails too, and the error is "Pixel 
  //rappresentation  non found" but I have set the Pixel rep with 
  //dataset-&gt;putAndInsertUint16(DCM_PixelRepresentation, infoDicom.pixelrapresentation);
  
     OFCondition status = fileformat.saveFile("test1.dcm", EXS_JPEGProcess14SV1);
  
       DJEncoderRegistration::cleanup();
  
          if (status.bad())
          {
             int error = 0; //only for test
          }
  
          thefile.Write(pSorg, sizeBuffer); //previous code
       }
  </code></pre>
  
  <p>Actually I made test with image that have on one frame, so the for cycle is done only one time. I don't understand why if I choose <code>dataset-&gt;chooseRepresentation(EXS_LittleEndianImplicit, &amp;params);</code> or <code>dataset-&gt;chooseRepresentation(EXS_LittleEndianEXplicit, &amp;params);</code> works perfectly but not when I choose <code>dataset-&gt;chooseRepresentation(EXS_JPEGProcess14SV1, &amp;params);</code></p>
  
  <p>If I use the same image in the first application, I can compress the image without problems...</p>
  
  <p><strong>EDIT:</strong>  I think the main problem to solve is the <code>status = dataset-&gt;chooseRepresentation(EXS_JPEGProcess14SV1, &amp;rp_lossless)</code> that return "Tag not found". How can I know wich tag is missed?</p>
  
  <p><strong>EDIT2</strong>: As suggest in the DCMTK forum I have added the tag about the Bits Allocated and now <strong>works</strong> for few images, but non for all. For some images I have again "Tag not found": how can I know wich one of tags is missing? As a rule it's better insert all the tags?</p>