]> Creatis software - gdcm.git/blobdiff - Example/ToInTag.cxx
Deal with any type on input + cosmetics
[gdcm.git] / Example / ToInTag.cxx
index fbc2cc129703b2bb406d34db7083a07c175cffca..0515287754699e1406020ebc4258467d65de0605 100755 (executable)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: ToInTag.cxx,v $
   Language:  C++
-  Date:      $Date: 2007/05/29 10:36:10 $
-  Version:   $Revision: 1.12 $
+  Date:      $Date: 2009/01/19 17:05:13 $
+  Version:   $Revision: 1.21 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
 #include "gdcmFileHelper.h"
 #include "gdcmDirList.h"
 #include "gdcmDebug.h"
-#include "gdcmArgMgr.h"
 #include "gdcmUtil.h"
 #include "gdcmSerieHelper.h"
 
+#include "gdcmArgMgr.h"
+
 #include <iostream>
 
 /**
@@ -83,9 +84,9 @@ int main(int argc, char *argv[])
    "                  {  [keep= list of seriesNumber to process]              ",
    "                   | [drop= list of seriesNumber to ignore] }             ",
    "                  [taggrid] [skel]                                        ",
-   "                  [input = {ACR|DCM}]                                     ", 
+   "                  [input = {ACR|DCM|IDO}]                                 ", 
    "                  [extent=image suffix (.IMA, .NEMA, .DCM, ...)]          ",
-   "                  [listonly] [split]                                      ",
+   "                  [listonly] [split] [rubout]                             ",
    "                  [noshadowseq][noshadow][noseq] [verbose] [debug]        ",
    "                                                                          ",
    " dirout : will be created if doesn't exist                                ",
@@ -97,6 +98,7 @@ int main(int argc, char *argv[])
    "        e.g : 1030,1035,1043                                              ", 
    " taggrid : user knows all the images are 'grid' -ie: not 'col', not 'raw'-",
    " extent : DO NOT forget the leading '.' !                                 ",
+   " input : IDO when *realy* old libIDO images                               ",
    " skel: name skeleton eg : patName_1.nema -> skel=patName_                 ",
    " split: creates a tree-like structure of directories as :                 ",
    "        - Patient                                                         ",
@@ -104,6 +106,7 @@ int main(int argc, char *argv[])
    "        --- Position                                                      ",
    "        ---- Images (sorted by Trigger Time /                             ",
    "                               Encoding Direction (Row, Column)           ",
+   " rubout : user asks to rubout burnt-in image number                       ",
    " noshadowseq: user doesn't want to load Private Sequences                 ",
    " noshadow : user doesn't want to load Private groups (odd number)         ",
    " noseq    : user doesn't want to load Sequences                           ",
@@ -111,6 +114,18 @@ int main(int argc, char *argv[])
    " debug    : *developer*  wants to run the program in 'debug mode'         ",
    FINISH_USAGE
 
+
+   enum Index
+   { 
+      IND_PatientName,
+      IND_SerieInstanceUID,
+      IND_ImagePosition,
+      IND_TriggerTime,
+      IND_PhaseEncodingDirection,
+      IND_seriesDescription,
+      IND_FileName       
+   };
+   
    std::cout << "inside ToInTag" << std::endl;
    
    // ----- Initialize Arguments Manager ------
@@ -125,10 +140,10 @@ int main(int argc, char *argv[])
    }
 
    const char *dirNamein;   
-   dirNamein  = am->ArgMgrGetString("dirin","."); 
+   dirNamein  = am->ArgMgrGetString("dirin",".");
 
    const char *dirNameout;   
-   dirNameout  = am->ArgMgrGetString("dirout",".");  
+   dirNameout  = am->ArgMgrGetString("dirout",".");
    
    int loadMode = GDCM_NAME_SPACE::LD_ALL;
    if ( am->ArgMgrDefined("noshadowseq") )
@@ -147,7 +162,9 @@ int main(int argc, char *argv[])
    int verbose  = am->ArgMgrDefined("verbose");
    int split    = am->ArgMgrDefined("split");
    int listonly = am->ArgMgrDefined("listonly");
-         
+   
+   bool rubout = ( 0 != am->ArgMgrDefined("rubout") ); 
+           
    int nbSeriesToKeep;
    int *seriesToKeep = am->ArgMgrGetListOfInt("keep", &nbSeriesToKeep);
    int nbSeriesToDrop;
@@ -160,14 +177,15 @@ int main(int argc, char *argv[])
       return 0;         
    }
    
-   int taggrid = am->ArgMgrDefined("taggrid");
-   
-   int hasSkel = am->ArgMgrDefined("skel");
+   bool taggrid = ( 0 != am->ArgMgrDefined("taggrid") );
+      
+   bool hasSkel = ( 0 != am->ArgMgrDefined("hasSkel") );    
    const char *skel;
    if (hasSkel)
-      skel = am->ArgMgrGetString("skel");   
+      skel = am->ArgMgrGetString("skel");
       
    const char *extent  = am->ArgMgrGetString("extent",".DCM");
+   const char *input   = am->ArgMgrGetString("input","DCM");
    
    // if unused Param we give up
    if ( am->ArgMgrPrintUnusedLabels() )
@@ -217,6 +235,8 @@ int main(int argc, char *argv[])
    {
        std::cout << "Output Directory [" << dirNameout << "] already exists; Used as is." << std::endl;
    }
     
    std::string strDirNamein(dirNamein);
    GDCM_NAME_SPACE::DirList dirList(strDirNamein, true); // get recursively the list of files
@@ -248,7 +268,7 @@ int main(int argc, char *argv[])
    std::vector<std::string> tokensForFileName;
    
    // For Siemens pb, we need Manufacturer's Model Name
-   
+   // (We read only the first file, to know)   
    GDCM_NAME_SPACE::DirListType::iterator it1 = fileNames.begin();
    f = GDCM_NAME_SPACE::File::New();
    f->SetLoadMode(GDCM_NAME_SPACE::LD_ALL);
@@ -282,19 +302,19 @@ int main(int argc, char *argv[])
    if (verbose)
       std::cout << "------------------Print Break levels-----------------" << std::endl;
 
-   std::string userFileIdentifier; 
+   std::string userFileIdentifier;
    SortedFiles sf;
 
    s->AddSeriesDetail(0x0010, 0x0010, false); // Patient's Name
    // for Siemens TrioTim, don't deal with 'Series Instance UID'
-   if ( !GDCM_NAME_SPACE::Util::DicomStringEqual(modelName,"TrioTim") ) 
+   if ( !GDCM_NAME_SPACE::Util::DicomStringEqual(modelName,"TrioTim") )
       s->AddSeriesDetail(0x0020, 0x000e, false); // Series Instance UID
    else
       s->AddSeriesDetail(0x9999, 0x9999, false); // dirty trick to ignore 'Series Instance UID'
-   s->AddSeriesDetail(0x0020, 0x0032, false); // Image Position (Patient)     
-   s->AddSeriesDetail(0x0018, 0x1060, true);  // Trigger Time (true: convert to keep numerical order)
-   s->AddSeriesDetail(0x0018, 0x1312, false); // In-plane Phase Encoding Direction 
-   s->AddSeriesDetail(0x0008, 0x103e, false); // Series Description (special Siemens ...)  
+      s->AddSeriesDetail(0x0020, 0x0032, false); // Image Position (Patient)
+      s->AddSeriesDetail(0x0018, 0x1060, true);  // Trigger Time (true: convert to keep numerical order)
+      s->AddSeriesDetail(0x0018, 0x1312, false); // In-plane Phase Encoding Direction
+      s->AddSeriesDetail(0x0008, 0x103e, false); // Series Description (special Siemens ...)
 
    //uint8_t *imageData; // Useless : pixels will not be loaded 
                          //          (images are overwritten)
@@ -315,7 +335,7 @@ int main(int argc, char *argv[])
       // keep only requested Series      
       bool keep = false;
       if (nbSeriesToKeep != 0)
-      {     
+      {
          strSeriesNumber = f->GetEntryString(0x0020, 0x0011 );
          seriesNumber = atoi( strSeriesNumber.c_str() );
          for (j=0;j<nbSeriesToKeep; j++)
@@ -351,16 +371,20 @@ int main(int argc, char *argv[])
            f->Delete();
            continue;
         }
-      }      
+      }
 
-      userFileIdentifier=s->CreateUserDefinedFileIdentifier(f); 
+      userFileIdentifier=s->CreateUserDefinedFileIdentifier(f);
       tokens.clear();
-      GDCM_NAME_SPACE::Util::Tokenize (userFileIdentifier, tokens, token); 
-   
+      GDCM_NAME_SPACE::Util::Tokenize (userFileIdentifier, tokens, token);
+
       int imageNum; // Within FileName
       char newName[1024];
-      
-      if ( tokens[3] == "gdcmUnfound")  // sometimes Trigger Time is not found. CreateUserDefinedFileIdentifier is not aware of the pb.
+
+      // sometimes Trigger Time is not found.
+      // CreateUserDefinedFileIdentifier is not aware of the pb.
+      // We use File name instead (hope it's significant)
+
+      if ( tokens[IND_TriggerTime] == GDCM_NAME_SPACE::GDCM_UNFOUND)
       {
          ///this is a trick to build up a lexicographical compliant name :
          ///     eg : fich001.ima vs fich100.ima as opposed to fich1.ima vs fich100.ima
@@ -371,16 +395,25 @@ int main(int argc, char *argv[])
             imageNum = atoi ( tokensForFileName[0].c_str() );
             // probabely we could write something much more complicated using C++ !
             sprintf (newName, "%s%06d%s", skel, imageNum, extent);
-            tokens[3] = newName;
+            tokens[IND_TriggerTime] = newName;
             tokensForFileName.clear();    
          }
          else
-            tokens[3] = name;
+            tokens[IND_TriggerTime] = name;
+
+         // Patient's Name
+         // Series Instance UID
+         // Image Position (Patient)
+         // Trigger Time
+         // In-plane Phase Encoding Direction
+         // Series Description
+         // FileName
  
-         userFileIdentifier = tokens[0] + token + tokens[1] + token + tokens[2] + token 
-                    + tokens[3] + token + tokens[4] + token + tokens[5] + token;
-      }   
+         userFileIdentifier = tokens[IND_PatientName] + token + tokens[IND_SerieInstanceUID] + token + tokens[IND_ImagePosition] + token 
+                    + tokens[IND_TriggerTime] + token + tokens[IND_PhaseEncodingDirection] + token + tokens[IND_seriesDescription] + token
+                    + tokens[IND_FileName] + token;
+      }
+         
       if (verbose) 
          std::cout << "[" << userFileIdentifier  << "] : " << *it << std::endl;
                
@@ -430,26 +463,27 @@ int main(int argc, char *argv[])
 
    std::string defaultStudyUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
    std::string defaultSerieUID;
-     
+
    for (it2 = sf.begin() ; it2 != sf.end(); ++it2)
    {  
       currentFile = it2->second;
        
       fullFilename =  currentFile->GetFileName();
-      lastFilename =  GDCM_NAME_SPACE::Util::GetName( fullFilename ); 
+      lastFilename =  GDCM_NAME_SPACE::Util::GetName( fullFilename );
+      if (verbose) 
       std::cout <<" ------------------------------------------------------------------------------" 
                 << std::endl << " Deal with [" << it2->first << "] : ["<<fullFilename << "]" 
                 << std::endl;
      
       tokens.clear();
       GDCM_NAME_SPACE::Util::Tokenize (it2->first, tokens, token);
-      
-      currentPatientName            = tokens[0];
-      currentSerieInstanceUID       = tokens[1];
-      currentImagePosition          = tokens[2];
-      currentTriggerTime            = tokens[3];
-      currentPhaseEncodingDirection = tokens[4];
-      seriesDescription             = tokens[5];  // For Siemens pb
+   
+      currentPatientName            = tokens[IND_PatientName];
+      currentSerieInstanceUID       = tokens[IND_SerieInstanceUID];
+      currentImagePosition          = tokens[IND_ImagePosition];
+      currentTriggerTime            = tokens[IND_TriggerTime];
+      currentPhaseEncodingDirection = tokens[IND_PhaseEncodingDirection];
+      seriesDescription             = tokens[IND_seriesDescription];  // For Siemens pb
 
       if ( currentImagePosition[0] == '-')
           currentImagePosition[0] = 'M';
@@ -463,21 +497,20 @@ int main(int argc, char *argv[])
          currentFile->InsertEntryString("0.\\0.\\0.",0x0020, 0x0032, "DS" );
       }
 
-      // Add a default ImagePositionPatient to avoid confusion at post processing time
+      // Add a default ImageOrientationPatient to avoid confusion at post processing time
       if ( currentFile->GetEntryString(0x0020,0x0037) == GDCM_NAME_SPACE::GDCM_UNFOUND && 
            currentFile->GetEntryString(0x0020,0x0035) == GDCM_NAME_SPACE::GDCM_UNFOUND )
       {
          currentFile->InsertEntryString("1.\\0.\\0.\\0.\\1.\\0.",0x0020, 0x0037, "DS" );
       }
-      
+
       if (previousPatientName != currentPatientName)
       {      
-         if ( currentFile->GetEntryString(0x0020,0x000d) == GDCM_NAME_SPACE::GDCM_UNFOUND)
+         if ( currentFile->GetEntryString(0x0020,0x000d) == GDCM_NAME_SPACE::GDCM_UNFOUND) // Study UID
          {
             if (verbose)   
                std::cout << "--- new  Study UID created" << std::endl;
             defaultStudyUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
-            currentFile->InsertEntryString(defaultStudyUID, 0x0020, 0x000d, "UI" );
          }
   
          previousPatientName = currentPatientName;
@@ -499,12 +532,14 @@ int main(int argc, char *argv[])
               system ( systemCommand.c_str() );
          }
       }
+      currentFile->InsertEntryString(defaultStudyUID, 0x0020, 0x000d, "UI" );
+      
 
       if ( GDCM_NAME_SPACE::Util::DicomStringEqual(modelName,"TrioTim") ) // for Siemens TrioTim , don't deal with 'Series Instance UID'
 
       if (previousSerieInstanceUID != currentSerieInstanceUID)
       {        
-         if (verbose)   
+         if (verbose)
             std::cout << "==== === new Serie [" << currentSerieInstanceUID << "]"
                       << std::endl;
       
@@ -513,9 +548,9 @@ int main(int argc, char *argv[])
             if (verbose)   
                std::cout << "--- --- new  Serie UID created" << std::endl;
             defaultSerieUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
-            currentFile->InsertEntryString(defaultSerieUID, 0x0020, 0x000e, "UI" );
-         }       
-      
+           // currentFile->InsertEntryString(defaultSerieUID, 0x0020, 0x000e, "UI" );
+         }
+
          if (split)
          {
              currentSerieWriteDir  = currentPatientWriteDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR
@@ -527,6 +562,8 @@ int main(int argc, char *argv[])
          previousImagePosition          = ""; //currentImagePosition;
          previousPhaseEncodingDirection = ""; //currentPhaseEncodingDirection;
       }
+      currentFile->InsertEntryString(defaultSerieUID, 0x0020, 0x000e, "UI" );
+            
       // end of modelName != "TrioTim "
    
       if (previousImagePosition != currentImagePosition)
@@ -555,7 +592,7 @@ int main(int argc, char *argv[])
 /*
       if (previousPhaseEncodingDirection != currentPhaseEncodingDirection)
       {        
-         if (verbose)   
+         if (verbose)
             std::cout << "==== === === === new PhaseEncodingDirection [" 
                       << currentPhaseEncodingDirection  << "]" << std::endl;
       
@@ -565,12 +602,12 @@ int main(int argc, char *argv[])
                              + GDCM_NAME_SPACE::GDCM_FILESEPARATOR
                              + currentPhaseEncodingDirection;
              systemCommand   = "mkdir " + currentPhaseEncodingDirectionWriteDir;     
-             system (systemCommand.c_str());     
-         }      
-    
+             system (systemCommand.c_str());
+         }
+
          previousPhaseEncodingDirection = currentPhaseEncodingDirection;
-      } 
-*/    
+      }
+*/
    
       if ( GDCM_NAME_SPACE::Debug::GetDebugFlag())
          std::cout << "--- --- --- --- --- " << it2->first << "  " 
@@ -614,9 +651,9 @@ int main(int argc, char *argv[])
 
          if ( GDCM_NAME_SPACE::Util::DicomStringEqual(modelName,"TrioTim") )  
          {
-            if (seriesDescription.find("90", 0) != -1)
+            if (seriesDescription.find("90", 0) != std::string::npos)
                chSessionIndex = "1";  // 90 deg -> COL
-            else if (seriesDescription.find("0", 0)!= -1)
+            else if (seriesDescription.find("0", 0)!= std::string::npos)
                chSessionIndex = "2";  // 0 deg -> ROW
             else
             {
@@ -686,8 +723,8 @@ int main(int argc, char *argv[])
             std::cout << "Duplicate ImageOrientation into ImageOrientationPatient" << std::endl;          
          currentFile->InsertEntryString(currentFile->GetEntryString(0x0020, 0x0035), 0x0020, 0x0037, "DS" );       
       }
-                
-      if (taggrid)
+       
+      if (taggrid  || strcmp(input, "IDO")==0 || strcmp(input, "ido")==0 )
          frameIndex++;
       else     
       {     
@@ -718,13 +755,15 @@ int main(int argc, char *argv[])
       uint8_t *imageData = fh->GetImageDataRaw(); // Don't convert (Gray Pixels + LUT) into (RGB pixels) ?!?
       fh->SetWriteTypeToDcmExplVR();     
       
-      // Put to Black the burnt-in number.
-      nX = currentFile->GetXSize();
-      nY = currentFile->GetYSize();
-      for(int y=nY-15; y<nY; y++)
-         for(int x=nX/3; x<nX/2+50; x++)
-           imageData[ y*nX*2 + x ] = 0;
-
+      if (rubout) {
+         // Put to Black the burnt-in number.
+         nX = currentFile->GetXSize();
+         nY = currentFile->GetYSize();
+         for(int y=nY-15; y<nY; y++)
+            for(int x=nX/3; x<nX/2+50; x++)
+              imageData[ y*nX*2 + x ] = 0;
+      }
+      
       // We didn't make any computation on the pixels -> keep unchanged the following :
       // 'Media Storage SOP Class UID' (0x0002,0x0002)
       // 'SOP Class UID'               (0x0008,0x0016)