]> Creatis software - gdcm.git/blobdiff - Example/WriteDicomAsJPEG.cxx
Fix mistypings
[gdcm.git] / Example / WriteDicomAsJPEG.cxx
index b6187943aaee99d50605852f3bfcd08ee7340064..b8d52fbc2371ec7142346c44624329428eb244cd 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: WriteDicomAsJPEG.cxx,v $
   Language:  C++
-  Date:      $Date: 2007/08/24 10:48:08 $
-  Version:   $Revision: 1.16 $
+  Date:      $Date: 2007/08/29 08:13:40 $
+  Version:   $Revision: 1.20 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -27,7 +27,7 @@ int main(int argc, char *argv[])
   if( argc < 2)
     {
     std::cerr << argv[0] << " inputfilename.dcm [ outputfilename.dcm"
-              << "quality debug]\n";
+              << " quality debug]\n";
     return 1;
     }
 
@@ -54,15 +54,16 @@ int main(int argc, char *argv[])
    int xsize = f->GetXSize();
    int ysize = f->GetYSize();
    int zsize = f->GetZSize();
+   //tested->Print( std::cout );
 
    int samplesPerPixel = f->GetSamplesPerPixel();
-   size_t testedDataSize    = tested->GetImageDataSize();
-   std::cerr << "testedDataSize:" << testedDataSize << std::endl;
-   uint8_t *testedImageData = tested->GetImageData();
+   size_t testedDataSize    = tested->GetImageDataRawSize(); // Raw : Don't convert gray pixels+LUT to RBG pixels
+   uint8_t *testedImageData = tested->GetImageDataRaw();
    
-   if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )  
+   if( GDCM_NAME_SPACE::Debug::GetDebugFlag() ) { 
       tested->Print( std::cout );
-
+      std::cout << "-------------------------------------------------------------------------------" << std::endl;
+   }
 // Step 1 : Create the header of the new file
    GDCM_NAME_SPACE::File *fileToBuild = GDCM_NAME_SPACE::File::New();
    std::ostringstream str;
@@ -74,17 +75,17 @@ int main(int argc, char *argv[])
    str.str("");
    str << ysize;
    fileToBuild->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
+
    if(zsize>1)
    {
       str.str("");
       str << zsize;
       fileToBuild->InsertEntryString(str.str(),0x0028,0x0008,"IS"); // Number of Frames
    }
-   
+
    int bitsallocated = f->GetBitsAllocated();
-   int bitsstored = f->GetBitsStored();
-   int highbit = f->GetHighBitPosition();
+   int bitsstored    = f->GetBitsStored();
+   int highbit       = f->GetHighBitPosition();
    //std::string pixtype = f->GetPixelType();
    int sign = f->IsSignedPixelData();
 
@@ -110,17 +111,49 @@ int main(int argc, char *argv[])
    str << samplesPerPixel; //img.components;
    fileToBuild->InsertEntryString(str.str(),0x0028,0x0002,"US"); // Samples per Pixel
 
+// The image may be displayed uncorectly if these fields are missing
+
+   // Set the Pixel Aspect Ratio, if any
+   std::string par = f->GetEntryString(0x0028,0x0034);
+   std::cerr <<"Pixel Aspect Ratio [" << par << "]" << std::endl;
+   if ( par != GDCM_NAME_SPACE::GDCM_UNFOUND )
+      fileToBuild->InsertEntryString(par,0x0028,0x0034,"IS"); // Pixel Aspect Ratio
+
+   // Set the Modality, if any
+   std::string modality = f->GetEntryString(0x0008,0x0060);
+   std::cerr <<"Modality [" << modality << "]" << std::endl;
+   if ( modality != GDCM_NAME_SPACE::GDCM_UNFOUND )
+      fileToBuild->InsertEntryString(modality,0x0008,0x0060,"CS"); // Modality
+
+   // Set the Media Storage SOP Class UID, if any
+   std::string mssop = f->GetEntryString(0x0002,0x0002);
+   std::cerr <<"Media Storage SOP Class UID [" << mssop << "]" << std::endl;
+   if ( mssop != GDCM_NAME_SPACE::GDCM_UNFOUND )
+      fileToBuild->InsertEntryString(mssop,0x0002,0x0002,"UI"); // Media Storage SOP Class UID
+
+   // This one is mandatory to deal with Pixel Aspect Ratio, in ultrasound images !
+   // Set the SOP Class UID, if any
+   std::string sop = f->GetEntryString(0x0008,0x0016);
+   std::cerr <<"SOP Class UID [" << sop << "]" << std::endl;
+   if ( sop != GDCM_NAME_SPACE::GDCM_UNFOUND )
+      fileToBuild->InsertEntryString(sop,0x0008,0x0016,"UI"); // SOP Class UID
+     
 // Step 2 : Create the output image
    size_t size = xsize * ysize * zsize
                * samplesPerPixel  * bitsallocated / 8;
 
    GDCM_NAME_SPACE::FileHelper *fileH = GDCM_NAME_SPACE::FileHelper::New(fileToBuild);
 
-   assert( size == testedDataSize );
+   // Consider that pixels are unmodified
+   fileH->SetContentType(GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE);
+   std::cerr << "xsize " << xsize << " ysize " << ysize << " zsize " << zsize << " samplesPerPixel " << samplesPerPixel
+             << " bitsallocated " << bitsallocated << std::endl;
+   std::cerr << "size " << size << " testedDataSize " << testedDataSize <<
+                 std::endl;
+   assert(abs (size-testedDataSize) <= 1 );
    fileH->SetWriteTypeToJPEG(  );
-
    //fileH->SetImageData(testedImageData, testedDataSize);
-   
+
    // SetUserData will ensure the compression
    fileH->SetUserData(testedImageData, testedDataSize);
    if( !fileH->Write(outfilename) )