]> Creatis software - gdcm.git/commitdiff
Fix troubles in AnonymizeMultiPatient.
authorjpr <jpr>
Wed, 31 May 2006 16:16:04 +0000 (16:16 +0000)
committerjpr <jpr>
Wed, 31 May 2006 16:16:04 +0000 (16:16 +0000)
(It ensures (hope so) that all the Patient names will be different
after anonymization.
It will probabely replace AnonymizeNoLoad

Example/AnonymizeMultiPatient.cxx

index 4b57eb582d941a3be7edf0343c6fd1010f613877..82aae27d1e0df98b56f0f6100fe68ee0df749951 100755 (executable)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: AnonymizeMultiPatient.cxx,v $
   Language:  C++
-  Date:      $Date: 2006/05/30 08:26:36 $
-  Version:   $Revision: 1.1 $
+  Date:      $Date: 2006/05/31 16:16:04 $
+  Version:   $Revision: 1.2 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
      PURPOSE.  See the above copyright notices for more information.
                                                                                 
 =========================================================================*/
-#include "gdcmFile.h"
-#include "gdcmFileHelper.h"
-#include "gdcmCommon.h"
-#include "gdcmUtil.h"
-#include "gdcmDebug.h"
-#include "gdcmDirList.h"
-
+#include "gdcmDocEntry.h"
 #include "gdcmDicomDir.h"
 #include "gdcmDicomDirPatient.h"
 #include "gdcmDicomDirStudy.h"
+#include "gdcmDicomDirVisit.h"
 #include "gdcmDicomDirSerie.h"
 #include "gdcmDicomDirImage.h"
+#include "gdcmDirList.h"
+#include "gdcmDebug.h"
+#include "gdcmUtil.h"
+#include "gdcmFile.h"
+#include "gdcmFileHelper.h"
 
 #include "gdcmArgMgr.h"
 
 #include <iostream>
 
+/**
+  * \brief   Explores recursively the given directory
+  *          orders the gdcm-readable found Files
+  *          according their Patient/Study/Serie/Image characteristics
+  *          and anomymizes (NoLoad) them, creates a different name for each Patient.
+  */  
 
-int main(int argc, char *argv[])
+int main(int argc, char *argv[]) 
 {
    START_USAGE(usage)
    " \n AnonymizeMultiPatient :\n                                             ",
    " AnonymizeMultiPatient a full gdcm-readable Dicom image                   ",
    "         Warning : the image is OVERWRITTEN                               ",
    "                   to preserve image integrity, use a copy.               ",
-   " usage: AnonymizeMultiPatient dirin=inputDirectoryName dirout=outputDirectoryName",
+   " usage: AnonymizeMultiPatient dirin=inputDirectoryName                    ",
    "       listOfElementsToRubOut : group-elem,g2-e2,... (in hexa, no space)  ",
    "                                of extra Elements to rub out              ",
    "       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                   ",
-   "       verbose  : user wants to run the program in 'verbose mode'         ",   
+   "       verbose    : user wants to run the program in 'verbose mode'       ",   
    "       debug      : user wants to run the program in 'debug mode'         ",   
    FINISH_USAGE
+   
+
 
    // ----- Initialize Arguments Manager ------   
    gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
@@ -59,28 +67,22 @@ int main(int argc, char *argv[])
       delete am;
       return 0;
    }
-   const char *dirName  = am->ArgMgrGetString("dirin");
-   if ( dirName == NULL )
+   const char * name  = am->ArgMgrGetString("dirin");
+   if ( name == NULL )
    {
       delete am;
       return 0;
    }
    
-   /*
-   char *outputDirName = am->ArgMgrWantString("dirout",usage);
-   if ( outputDirName == NULL )
-   {
-      delete am;
-      return 0;
-   }
- */
-   
+   std::string dirName = name;
    bool verbose  = am->ArgMgrDefined("verbose");
    
    if (am->ArgMgrDefined("debug"))
       gdcm::Debug::DebugOn();
 
    int loadMode = gdcm::LD_ALL;
    if ( am->ArgMgrDefined("noshadowseq") )
       loadMode |= gdcm::LD_NOSHADOWSEQ;
    else 
@@ -91,10 +93,11 @@ int main(int argc, char *argv[])
          loadMode |= gdcm::LD_NOSEQ;
    }
 
+
    int rubOutNb;
    uint16_t *elemsToRubOut = am->ArgMgrGetXInt16Enum("rubout", &rubOutNb);
  
-   // if unused Param we give up
+   // ----------- if unused Param we give up
    if ( am->ArgMgrPrintUnusedLabels() )
    { 
       am->ArgMgrUsage(usage);
@@ -103,145 +106,78 @@ int main(int argc, char *argv[])
    }
 
    delete am;  // we don't need Argument Manager any longer
-   
+
   // ---------------------------------------------------------- 
   
-     // ----- Begin Processing -----
+
+   // ----- Begin Processing -----
 
    gdcm::DicomDir *dcmdir;
 
    // we ask for Directory parsing
 
    dcmdir = gdcm::DicomDir::New( );
-
    dcmdir->SetLoadMode(loadMode);
    dcmdir->SetDirectoryName(dirName);
    dcmdir->Load();
 
-
+   if ( verbose )
+      std::cout << "======================= End Parsing Directory" << std::endl;
+      
+    // ----- Check the result
+    
+   if ( !dcmdir->GetFirstPatient() ) 
+   {
+      std::cout << "No patient found (?!?). Exiting."
+                << std::endl;
+      dcmdir->Delete();
+      return 1;
+   }
 
    gdcm::DicomDirPatient *pa;
    gdcm::DicomDirStudy *st;
    gdcm::DicomDirSerie *se;
    gdcm::DicomDirVisit *vs;
-   gdcm::DicomDirImage *im;      
-  
-   // Test if the DicomDir contains any Patient
-   pa = dcmdir->GetFirstPatient();
-   if ( pa == 0)
-   {
-      std::cout<<"          DicomDir '"<< dirName
-               <<" has no patient"<<std::endl
-               <<"          ...Failed"<<std::endl;
-
-      dcmdir->Delete();
-      return 1;
-   } 
-   
-      if ( verbose )
-      std::cout << "-------------------------- Directory [" << dirName << "] parsed" << std::endl;
-
-// Since files may be help in a Directory tree-like structure
-// we should have to duplicate this structure.
-// No time to code that stuff
-//  --> We 'AnonymizeNoLoad' (overwrite the files)
+   gdcm::DicomDirImage *im;
 
-// Do not remove the commented out lines.
-// They will be usefull in a further program. 
-/*
-   std::string systemCommand;
-   
-   std::cout << "Check for output directory :[" << outputDirName << "]."
-             <<std::endl;
-   if ( ! gdcm::DirList::IsDirectory(outputDirName) )    // dirout not found
-   {
-      std::string strDirNameout(outputDirName);          // to please gcc 4
-      systemCommand = "mkdir " +strDirNameout;        // create it!
+   std::string codedName; 
+   std::string fullFileName;
+   std::string patName;
+      
+   gdcm::File *f;
+  
+   pa = dcmdir->GetFirstPatient(); 
+   while ( pa )
+   {  // on degouline les PATIENT du DICOMDIR
+      patName = pa->GetEntryString(0x0010, 0x0010);
+      codedName = "g^" + gdcm::Util::ConvertToMD5(patName);
       if (verbose)
-         std::cout << systemCommand << std::endl;
-      system (systemCommand.c_str());
-      if ( ! gdcm::DirList::IsDirectory(outputDirName) ) // be sure it worked
-      {
-          std::cout << "KO : not a dir : [" << outputDirName << "] (creation failure ?)" << std::endl;
-          exit(0);
-      }
-      else
-      {
-        std::cout << "Directory [" << outputDirName << "] created." << std::endl;
-      }
-   }
-   else
-   {
-       std::cout << "Output Directory [" << outputDirName << "] already exists; Used as is." << std::endl;
-   }
-*/
-
-  gdcm::File *f;
-  gdcm::FileHelper *fh;
- // std::string outputFileName;
-
-  std::string codedName; 
-  while (pa)
-  {
-     if (verbose)
-     {
-        std::cout << "Pat.Name:[" << pa->GetEntryString(0x0010, 0x0010) <<"]"; // Patient's Name
-        std::cout << " Pat.ID:[";
-        std::cout << pa->GetEntryString(0x0010, 0x0020) << "]" << std::endl; // Patient ID
-     }     
-     std::string patName = pa->GetEntryString(0x0010, 0x0010);
-     //codedName = "g^" + gdcm::Util::ConvertToMD5(patName); // just to be sure MD5 is not guilty
-     std::string fullFileName;
-     if (verbose)
-        std::cout << patName << " --> " << codedName << std::endl;
-         
-     st = pa->GetFirstStudy();
+         std::cout << patName << " --> " << codedName << std::endl;         
+      st = pa->GetFirstStudy();
       while ( st ) 
       { // on degouline les STUDY de ce patient
-         if (verbose)
-         {
-            std::cout << "--- Stud.descr:["    << st->GetEntryString(0x0008, 0x1030) << "]"; // Study Description    
-            std::cout << " Stud.ID:["          << st->GetEntryString(0x0020, 0x0010) << "]"; // Study ID
-            std::cout << std::endl;
-         }
          se = st->GetFirstSerie();
          while ( se ) 
          { // on degouline les SERIES de cette study
-            if (verbose)
-            {
-               std::cout << "--- --- Ser.Descr:["<< se->GetEntryString(0x0008, 0x103e) << "]";  // Series Description
-               std::cout << " Ser.nb:["          <<  se->GetEntryString(0x0020, 0x0011) << "]";  // Series number
-               std::cout << " Mod.:["      <<  se->GetEntryString(0x0008, 0x0060) << "]";  // Modality
-               std::cout << " Serie Inst.UID.:[" <<  se->GetEntryString(0x0020, 0x000e) << "]";  // Series Instance UID
-               std::cout << std::endl; 
-            }
-  
             im = se->GetFirstImage();
-            std::string ReferencedFileID;
             while ( im ) 
-            { // on degouline les IMAGEs de cette serie
-               ReferencedFileID = im->GetEntryString(0x0004, 0x1500);
+            { // on degouline les Images de cette serie       
+               fullFileName = dirName;
+               fullFileName +=  gdcm::GDCM_FILESEPARATOR;
+               fullFileName += im->GetEntryString(0x0004, 0x1500);
                if (verbose)
-               {
-                  std::cout << "--- --- --- "<< " IMAGE Ref. File ID :[" << ReferencedFileID 
-                            << "]" << std::endl; // File name (Referenced File ID)
-               }       
+                  std::cout << "FileName " << fullFileName << std::endl;
+
                f = gdcm::File::New( );
                f->SetLoadMode(loadMode);
-               fullFileName = dirName;
-       
-               std::cout << "fullFileName (1) " << fullFileName << std::endl;
-               std::cout << "ReferencedFileID " << ReferencedFileID << std::endl;
-               fullFileName = fullFileName + "/" + ReferencedFileID;       
-               std::cout << "fullFileName (2) " << fullFileName << std::endl;
-
                f->SetFileName( fullFileName );
-       
                if ( !f->Load() )
-                   std::cout << "Load failed for [" << fullFileName << "]" << std::endl; 
+                 std::cout << "Load failed for [" << fullFileName << "]" << std::endl; 
                else
-                  std::cout << "Load successed for [" << fullFileName << "]" << std::endl; 
-       
+                  if (verbose)
+                     std::cout << "Load successed for [" << fullFileName << "]" << std::endl;
+
                // 
                //  Choose the fields to anonymize.
                // 
@@ -264,83 +200,38 @@ int main(int argc, char *argv[])
                // Study Instance UID
                // we may not brutaly overwrite it
                //f->AddAnonymizeElement( 0x0020, 0x000d, "9.99.999.9999" );
-  
+   
                // Telephone
                f->AddAnonymizeElement(0x0010, 0x2154, "3615" );
 
                // deal with user defined Elements set
 
-std::cout << "rubOutNb " << rubOutNb << std::endl;
                for (int ri=0; ri<rubOutNb; ri++)
                {
                   f->AddAnonymizeElement((uint32_t)elemsToRubOut[2*ri], 
-                       (uint32_t)elemsToRubOut[2*ri+1],"*" ); 
+                                         (uint32_t)elemsToRubOut[2*ri+1],"*" ); 
                }
-      
-               // The gdcm::File is modified in memory
-               // f->AnonymizeFile();    
-
-               // 
-               //      Overwrite the file
-               // 
-               // The gdcm::File remains untouched in memory    
-               f->AnonymizeNoLoad();     
-
-               // ============================================================
-               //               Write a new file
-               // ============================================================
-
-               // 
-               //      No need to load the pixels in memory.
-               //      File will be overwritten
-
-               // Do not remove the commented out lines.
-               // They will be usefull in a further program.
-               // 
-/*     
-               // Get the Pixels
-               fh = gdcm::FileHelper::New(f);
-
-               // unit8_t DOESN'T mean it's mandatory for the image to be a 8 bits one !
-               // Feel free to cast if you know it's not. 
-
-               uint8_t *imageData = fh->GetImageData();
-
-               if ( imageData == 0 )
-               {
-                  std::cerr << "Sorry, Pixels of" << im->GetEntryString(0x0004, 0x1500) <<"  are not "
-                      << " gdcm-readable."       << std::endl
-                      << "Use AnonymizeNoLoad" << std::endl;
-                  f->Delete();
-                  fh->Delete();
-                  break;
-                } 
 
-                     // Since we just Anonymized the file, we *know* no modification 
-                     // was performed on the pixels.
-                     // The written image will not appear as a 'Secondary Captured image'
-                     // nor as a DERIVED one
-
-                     fh->SetContentType(gdcm::UNMODIFIED_PIXELS_IMAGE);
-                     outputFileName = outputDirName + "/" + im->GetEntryString(0x0004, 0x1500);
-                     fh->WriteDcmExplVR(outputFileName);
-                     std::cout <<"End Anonymize" << std::cout;
-
-                     fh->Delete();       
-*/
+              // 
+              //      Overwrite the file
+              // 
+              // The gdcm::File remains untouched in memory    
+   
+              f->AnonymizeNoLoad();     
 
-                     f->Delete();
-                     f->ClearAnonymizeList();
-     
-                     im = se->GetNextImage();   
-                  }
-                  se = st->GetNextSerie();   
-               }  
-               st = pa->GetNextStudy();
-            }         
-     
+              f->ClearAnonymizeList();
+              f->Delete();
+              im = se->GetNextImage();   
+           }
+           se = st->GetNextSerie();   
+        }
+        st = pa->GetNextStudy();
+     }     
      pa = dcmdir->GetNextPatient();    
-  }
-  
-}
+   }
 
+   dcmdir->Delete();        
+   return 0;
+   
+}