]> Creatis software - gdcm.git/blobdiff - Example/ToInTag.cxx
Fix message for View Position
[gdcm.git] / Example / ToInTag.cxx
index 73c51f10a28853cea18b19854961d6e81641c5b4..85b085875d622b8503d8575c10f256f16c850229 100755 (executable)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: ToInTag.cxx,v $
   Language:  C++
-  Date:      $Date: 2006/06/01 10:33:13 $
-  Version:   $Revision: 1.2 $
+  Date:      $Date: 2006/07/17 13:24:59 $
+  Version:   $Revision: 1.7 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -36,7 +36,6 @@
   *            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;
@@ -44,7 +43,7 @@ typedef std::map<std::string, gdcm::File*> SortedFiles;
 int main(int argc, char *argv[]) 
 {
    START_USAGE(usage)
-   " \n ToInTag :\n                                                  ",
+   " \n ToInTag :\n                                                           ",
    " - explores recursively the given directory,                              ",
    " - keeps the requested series/ drops the unrequested series               ",
    " - orders the gdcm-readable found Files according to their                ",
@@ -69,7 +68,7 @@ int main(int argc, char *argv[])
    "           0x0021, 0x1040 : 'FRAME INDEX'                                 ",
    "           0x0020, 0x0012 : 'SESSION INDEX'  (Acquisition Number)         ",
    " usage:                                                                   ",
-   " PhilipsToBrucker dirin=rootDirectoryName                                 ",
+   " ToInTag          dirin=rootDirectoryName                                 ",
    "                  dirout=outputDirectoryName                              ",
    "                  {  [keep= list of seriesNumber to process]              ",
    "                   | [drop= list of seriesNumber to ignore] }             ",
@@ -102,6 +101,8 @@ int main(int argc, char *argv[])
    " debug    : *developer*  wants to run the program in 'debug mode'         ",
    FINISH_USAGE
 
+   std::cout << "inside ToInTag" << std::endl;
+   
    // ----- Initialize Arguments Manager ------
       
    gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
@@ -214,6 +215,7 @@ int main(int argc, char *argv[])
    {
       std::cout << "------------List of found files ------------" << std::endl;
       dirList.Print();
+      std::cout << std::endl;
    }
    
    gdcm::DirListType fileNames;
@@ -267,7 +269,10 @@ int main(int argc, char *argv[])
    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 
-      
+
+   //uint8_t *imageData; // Useless : pixels will not be loaded 
+                         //          (images are overwritten)
+         
    for (gdcm::DirListType::iterator it = fileNames.begin();  
                                     it != fileNames.end();
                                   ++it)
@@ -357,6 +362,9 @@ int main(int argc, char *argv[])
       // storing in a map ensures automatic sorting !      
       sf[userFileIdentifier] = f;
    }
+   
+   if (verbose)
+      std::cout << "  " << std::endl;
       
    std::string fullFilename, lastFilename;
    std::string previousPatientName, currentPatientName;
@@ -364,6 +372,8 @@ int main(int argc, char *argv[])
    std::string previousImagePosition, currentImagePosition;
    std::string previousPhaseEncodingDirection, currentPhaseEncodingDirection;
    std::string previousTriggerTime, currentTriggerTime;
+   
+   std::string currentStudyUID;   
       
    std::string writeDir, currentWriteDir;
    std::string currentPatientWriteDir, currentSerieWriteDir, 
@@ -382,10 +392,18 @@ int main(int argc, char *argv[])
    previousTriggerTime            = "";
    
    int sliceIndex = 0; // Is incremented *at the beginning* of processing
-   int frameIndex = 1;
+   int frameIndex;
+   if (taggrid)
+       frameIndex = 0;
+   else
+       frameIndex = 1;
+              
    int flag       = 0;
        
    gdcm::File *currentFile;
+
+   std::string defaultStudyUID =  gdcm::Util::CreateUniqueUID();
+   std::string defaultSerieUID; 
      
    for (it2 = sf.begin() ; it2 != sf.end(); ++it2)
    {  
@@ -393,7 +411,8 @@ int main(int argc, char *argv[])
        
       fullFilename =  currentFile->GetFileName();
       lastFilename =  gdcm::Util::GetName( fullFilename ); 
-      std::cout << "Rewrite [" <<lastFilename << "]" << std::endl;
+      std::cout << " --------------------------------------------------"
+                << " Rewrite [" <<lastFilename << "]" << std::endl;
      
       tokens.clear();
       gdcm::Util::Tokenize (it2->first, tokens, token);
@@ -410,7 +429,15 @@ int main(int argc, char *argv[])
           currentImagePosition[0] = 'P'; 
       
       if (previousPatientName != currentPatientName)
-      {
+      {      
+         if ( currentFile->GetEntryString(0x0020,0x000d) == gdcm::GDCM_UNFOUND)
+         {
+            if (verbose)   
+               std::cout << "--- new  Study UID created" << std::endl;
+            defaultStudyUID =  gdcm::Util::CreateUniqueUID();
+            currentFile->InsertEntryString(defaultStudyUID, 0x0020, 0x000d, "UI" );
+         }
+  
          previousPatientName = currentPatientName;
          if (verbose)   
             std::cout << "==== new Patient  [" << currentPatientName  << "]" << std::endl;
@@ -436,6 +463,15 @@ int main(int argc, char *argv[])
          if (verbose)   
             std::cout << "==== === new Serie [" << currentSerieInstanceUID << "]"
                       << std::endl;
+      
+         if ( currentFile->GetEntryString(0x0020,0x000e) == gdcm::GDCM_UNFOUND)
+         {
+            if (verbose)   
+               std::cout << "--- --- new  Serie UID created" << std::endl;
+            defaultSerieUID =  gdcm::Util::CreateUniqueUID();
+            currentFile->InsertEntryString(defaultSerieUID, 0x0020, 0x000e, "UI" );
+         }       
+      
          if (split)
          {
              currentSerieWriteDir  = currentPatientWriteDir + gdcm::GDCM_FILESEPARATOR
@@ -468,7 +504,9 @@ int main(int argc, char *argv[])
             sliceIndex = 1; // only *one* slice in a given directory
          else
             sliceIndex += 1;
-      }      
+      }
+      if (verbose)
+         std::cout << "Slice Index : " << sliceIndex << std::endl;      
 
 // We don't split on Row/Column!
 /*
@@ -535,9 +573,9 @@ int main(int argc, char *argv[])
             chSessionIndex = "1";
          }
       }
-       if (currentFile->IsVRCoherent(0x0020) == 1 )     
+      if (currentFile->IsVRCoherent(0x0020) == 1 )     
          currentFile->InsertEntryString(chSessionIndex, 0x0020, 0x0012, "  ");
-       else
+      else
          currentFile->InsertEntryString(chSessionIndex, 0x0020, 0x0012, "IS");
  
       // Deal with  0x0021, 0x1020 : 'SLICE INDEX'
@@ -557,7 +595,24 @@ int main(int argc, char *argv[])
   
       currentFile->InsertEntryString(strChSliceIndex, 0x0021, 0x1020, stringVR);
       currentFile->InsertEntryString(chFrameIndex,    0x0021, 0x1040, stringVR); 
+      std::string strImagePositionPatient    = currentFile->GetEntryString(0x0020, 0x0032 );
+      if (strImagePositionPatient == gdcm::GDCM_UNFOUND)
+      {
+         if (verbose)
+            std::cout << "Duplicate ImagePosition into ImagePositionPatient" << std::endl;
+         currentFile->InsertEntryString(currentFile->GetEntryString(0x0020, 0x0030), 0x0020, 0x0032, "DS" );
+      }  
       
+      std::string strImageOrientationPatient = f->GetEntryString(0x0020, 0x0037 );
+      if (strImageOrientationPatient == gdcm::GDCM_UNFOUND)
+      {
+         if (verbose)
+            std::cout << "Duplicate ImageOrientation into ImageOrientationPatient" << std::endl;      
+         currentFile->InsertEntryString(currentFile->GetEntryString(0x0020, 0x0035), 0x0020, 0x0037, "DS" );       
+      }
+                
       if (taggrid)
          frameIndex++;
       else     
@@ -572,6 +627,8 @@ int main(int argc, char *argv[])
             flag = 0;
          }
       } 
+      if (verbose)
+         std::cout << "Frame Index : " << frameIndex << std::endl; 
                  
       if (split)
       
@@ -592,13 +649,21 @@ int main(int argc, char *argv[])
       // Load the pixels in RAM.    
       
       fh = gdcm::FileHelper::New(currentFile);     
-      fh->GetImageDataRaw(); // Don't convert (Gray Pixels + LUT) into (RGB pixels) ?!?
+      uint8_t *imageData = fh->GetImageDataRaw(); // Don't convert (Gray Pixels + LUT) into (RGB pixels) ?!?
       fh->SetWriteTypeToDcmExplVR();
       // We didn't modify pixels -> keep unchanged the following :
       // 'Media Storage SOP Class UID' (0x0002,0x0002)
       // 'SOP Class UID'               (0x0008,0x0016)
       // 'Image Type'                  (0x0008,0x0008)
       // 'Conversion Type'             (0x0008,0x0064)
+      
+      // 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;   
+        
       fh->SetContentType(gdcm::UNMODIFIED_PIXELS_IMAGE);
       if (!fh->Write(fullWriteFilename))
       {