]> Creatis software - gdcm.git/blobdiff - Example/PhilipsToBrucker.cxx
ENH: vtkgdcmSerieViewer2 looks like it's completely duplicated code. oh god we love...
[gdcm.git] / Example / PhilipsToBrucker.cxx
index f8f9ee855e6f452482db4e096e4fad7cd7a710fd..a1cbe760c4ca4ff6492046818e8c62d46d797d71 100755 (executable)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: PhilipsToBrucker.cxx,v $
   Language:  C++
-  Date:      $Date: 2005/12/22 15:03:52 $
-  Version:   $Revision: 1.2 $
+  Date:      $Date: 2006/01/26 15:52:42 $
+  Version:   $Revision: 1.6 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -19,6 +19,7 @@
 #include "gdcmDicomDir.h"
 #include "gdcmDicomDirPatient.h"
 #include "gdcmFile.h"
+#include "gdcmFileHelper.h"
 #include "gdcmDirList.h"
 #include "gdcmDebug.h"
 #include "gdcmArgMgr.h"
   *            according to their Patient/Study/Serie/Image characteristics
   *          - fills a single level Directory with *all* the files,
   *            converted into a Brucker-like Dicom, Intags compliant
-  *          
+  * 
   */  
 
 typedef std::map<std::string, gdcm::File*> SortedFiles;
 
 int main(int argc, char *argv[]) 
 {
-
    START_USAGE(usage)
    " \n PhilipsToBrucker :\n                                                  ",
    " - explores recursively the given directory,                              ",
@@ -92,11 +92,11 @@ int main(int argc, char *argv[])
       return 0;
    }
 
-   char *dirNamein;   
-   dirNamein  = am->ArgMgrGetString("dirin",(char *)"."); 
+   const char *dirNamein;
+   dirNamein  = am->ArgMgrGetString("dirin","."); 
 
-   char *dirNameout;   
-   dirNameout  = am->ArgMgrGetString("dirout",(char *)".");  
+   const char *dirNameout;   
+   dirNameout  = am->ArgMgrGetString("dirout",".");  
    
    int loadMode = gdcm::LD_ALL;
    if ( am->ArgMgrDefined("noshadowseq") )
@@ -119,7 +119,6 @@ int main(int argc, char *argv[])
    int nbSeriesToDrop;
    int *seriesToDrop = am->ArgMgrGetListOfInt("drop", &nbSeriesToDrop);
  
-   std::cout << nbSeriesToKeep << "  " <<   nbSeriesToDrop << std::endl;
    if ( nbSeriesToKeep!=0 && nbSeriesToDrop!=0)
    {
       std::cout << "KEEP and DROP are mutually exclusive !" << std::endl;
@@ -127,7 +126,7 @@ int main(int argc, char *argv[])
       return 0;         
    }
    
-   char *extent  = am->ArgMgrGetString("extent",".DCM");
+   const char *extent  = am->ArgMgrGetString("extent",".DCM");
         
    // if unused Param we give up
    if ( am->ArgMgrPrintUnusedLabels() )
@@ -142,33 +141,48 @@ int main(int argc, char *argv[])
    
    if ( ! gdcm::DirList::IsDirectory(dirNamein) )
    {
-      std::cout << "not a directory : [" << dirNamein << "]" << std::endl;
+      std::cout << "KO : [" << dirNamein << "] is not a Directory : " << std::endl;
       exit(0);
    }
+   else
+   {
+      std::cout << "OK : [" << dirNamein << "]" << " is a Directory" << std::endl;
+   }
+
+
    std::string systemCommand;
-   if ( ! gdcm::DirList::IsDirectory(dirNameout) ) // dirout not found
+   if ( ! gdcm::DirList::IsDirectory(dirNameout) )     // dirout not found
    {
-      std::string strDirNameout(dirNameout); // to please gcc 4
-      systemCommand = "mkdir " +strDirNameout;      // create it!
+      std::string strDirNameout(dirNameout);          // to please gcc 4
+      systemCommand = "mkdir " +strDirNameout;        // create it!
+      if (verbose)
+         std::cout << systemCommand << std::endl;
       system (systemCommand.c_str());
       if ( ! gdcm::DirList::IsDirectory(dirNameout) ) // be sure it worked
       {
-          std::cout << "not a dir : [" << dirNameout << "]" << std::endl;
+          std::cout << "KO : not a dir : [" << dirNameout << "] (creation failure ?)" << std::endl;
           exit(0);
       }
+      else
+     {
+        std::cout << "Directory [" << dirNameout << "]" << " created" << std::endl;
+     }
+   }
+   else
+   {
+       std::cout << "Output Directory [" << dirNameout << "]" << " already exists; Used as is." << std::endl;
    }
-    
    std::string strDirNamein(dirNamein);
    gdcm::DirList dirList(strDirNamein, true); // get recursively the list of files
    
- /*  
-   std::cout << "---------------File list found ------------" << std::endl;
+/*   
+   std::cout << "---------------List of found files ------------" << std::endl;
    dirList.Print();
  */   
 
    gdcm::DirListType fileNames;
    fileNames = dirList.GetFilenames();
-   gdcm::SerieHelper *s;     // Needed only to may use SerieHelper::AddSeriesDetail()
+   gdcm::SerieHelper *s;              // Needed only to may use SerieHelper::AddSeriesDetail()
    s = gdcm::SerieHelper::New();
 
 /*       
@@ -180,8 +194,8 @@ int main(int argc, char *argv[])
 */
   
    gdcm::File *f;
-
-/*    
+   gdcm::FileHelper *fh;
+/*   
    std::cout << "---------------Print Unique Series identifiers---------"  
              << std::endl;     
    std::string uniqueSeriesIdentifier;
@@ -244,7 +258,10 @@ int main(int argc, char *argv[])
             }
          }
          if ( !keep)
-            continue; 
+         {
+            f->Delete();
+            continue;
+         } 
       }
       // drop all unrequested Series
       bool drop = false;
@@ -255,13 +272,16 @@ int main(int argc, char *argv[])
          for (j=0;j<nbSeriesToDrop; j++)
          {
             if(seriesNumber == seriesToDrop[j])
-            { 
+            {
                drop = true;
                break;
             }
         }
         if (drop)
+        {
+           f->Delete();
            continue;
+        }
       }      
 
       userFileIdentifier=s->CreateUserDefinedFileIdentifier(f);        
@@ -300,10 +320,7 @@ int main(int argc, char *argv[])
        
       fullFilename =  currentFile->GetFileName();
       lastFilename =  gdcm::Util::GetName( fullFilename ); 
-      if (verbose)
-         std::cout << "==== === === ===    " << it2->first << "  " 
-                   << (it2->second)->GetFileName() << " " 
-                   << gdcm::Util::GetName( fullFilename ) <<std::endl;
+
       
       tokens.clear();
       gdcm::Util::Tokenize (it2->first, tokens, "_");
@@ -319,14 +336,16 @@ int main(int argc, char *argv[])
          if (verbose)   
             std::cout << "==== new Patient " << currentPatientName << std::endl;
          previousPatientName            = currentPatientName;
-         previousSerieInstanceUID       = currentSerieInstanceUID;
-         previousImagePosition          = currentImagePosition;
-         previousPhaseEncodingDirection = currentPhaseEncodingDirection;
+         previousSerieInstanceUID       = ""; //currentSerieInstanceUID;
+         previousImagePosition          = ""; //currentImagePosition;
+         previousPhaseEncodingDirection = ""; //currentPhaseEncodingDirection;
  
          currentWriteDir = writeDir + currentPatientName;
          if ( ! gdcm::DirList::IsDirectory(currentWriteDir) )
            {
               systemCommand   = "mkdir " + currentWriteDir;
+              if (verbose)
+                 std::cout << systemCommand << std::endl;
               system ( systemCommand.c_str() );
          }
       }
@@ -337,17 +356,17 @@ int main(int argc, char *argv[])
             std::cout << "==== === new Serie " << currentSerieInstanceUID 
                       << std::endl;
          previousSerieInstanceUID       = currentSerieInstanceUID;
-         previousImagePosition          = currentImagePosition;
-         previousPhaseEncodingDirection = currentPhaseEncodingDirection;
+         previousImagePosition          = ""; //currentImagePosition;
+         previousPhaseEncodingDirection = ""; //currentPhaseEncodingDirection;
       }
 
       if (previousImagePosition != currentImagePosition)
       {        
          if (verbose)   
-            std::cout << "==== === === new Position " << currentImagePosition 
+            std::cout << "=== === === new Position " << currentImagePosition 
                       << std::endl;
          previousImagePosition          = currentImagePosition;
-         previousPhaseEncodingDirection = currentPhaseEncodingDirection;
+         previousPhaseEncodingDirection = ""; //currentPhaseEncodingDirection;
          sliceIndex += 1;
       }      
 
@@ -355,12 +374,17 @@ int main(int argc, char *argv[])
       {        
          if (verbose)   
             std::cout << "==== === === === new PhaseEncodingDirection " 
-                      << currentImagePosition << std::endl;
+                      << currentPhaseEncodingDirection << std::endl;
          previousPhaseEncodingDirection = currentPhaseEncodingDirection;
-      }
-      
-      frameIndex++;
-      
+      }      
+      frameIndex++; 
+      if (verbose)
+         std::cout << "--- --- --- --- --- " << (it2->second)->GetFileName() 
+                   << std::endl;
+      if ( gdcm::Debug::GetDebugFlag())
+         std::cout << "--- --- --- --- --- " << it2->first << "  " 
+                   << (it2->second)->GetFileName() << " " 
+                   << gdcm::Util::GetName( fullFilename ) << std::endl;           
       
       // Transform the image to be 'Brucker-Like'
       // ----------------------------------------   
@@ -372,7 +396,6 @@ int main(int argc, char *argv[])
       float pxSzY = currentFile->GetYSpacing();
       char fov[64];
       sprintf(fov, "%f\\%f",nX*pxSzX, nY*pxSzY);
-      std::cout << fov << std::endl;      
       currentFile->InsertEntryString(fov, 0x0019, 0x1000, "DS");
      
       // Deal with 0x0020, 0x0012 : 'SESSION INDEX'
@@ -394,11 +417,21 @@ int main(int argc, char *argv[])
       sprintf(chFrameIndex, "%04d", frameIndex);
       currentFile->InsertEntryString(chFrameIndex, 0x0021, 0x1040, "IS"); 
                     
-      std::string strExtent(extent);    
-      systemCommand  = "cp " + fullFilename + " " + currentWriteDir + 
-                       "/" + lastFilename + strExtent;
+      std::string strExtent(extent);
+      std::string fullWriteFilename = currentWriteDir + gdcm::GDCM_FILESEPARATOR + lastFilename + strExtent;
+      
+      /*           
+      systemCommand  = "cp " + fullFilename + " " + fullWriteFilename;
       std::cout << systemCommand << std::endl;
-      //system (  systemCommand.c_str() );
-           
+      system (  systemCommand.c_str() );
+      */
+            
+      // Load the pixels in RAM.      
+      
+      fh = gdcm::FileHelper::New(currentFile);     
+      fh->GetImageDataRaw(); // Don't convert (Gray Pixels + LUT) into (RGB pixels) ?!?
+      fh->SetWriteTypeToDcmExplVR();
+      fh->Write(fullWriteFilename);
+      fh->Delete();       
    }
  }