]> Creatis software - gdcm.git/blob - Example/AnonymizeMultiPatient.cxx
Fix troubles in AnonymizeMultiPatient.
[gdcm.git] / Example / AnonymizeMultiPatient.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: AnonymizeMultiPatient.cxx,v $
5   Language:  C++
6   Date:      $Date: 2006/05/31 16:16:04 $
7   Version:   $Revision: 1.2 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18 #include "gdcmDocEntry.h"
19 #include "gdcmDicomDir.h"
20 #include "gdcmDicomDirPatient.h"
21 #include "gdcmDicomDirStudy.h"
22 #include "gdcmDicomDirVisit.h"
23 #include "gdcmDicomDirSerie.h"
24 #include "gdcmDicomDirImage.h"
25 #include "gdcmDirList.h"
26 #include "gdcmDebug.h"
27 #include "gdcmUtil.h"
28 #include "gdcmFile.h"
29 #include "gdcmFileHelper.h"
30
31 #include "gdcmArgMgr.h"
32
33 #include <iostream>
34
35 /**
36   * \brief   Explores recursively the given directory
37   *          orders the gdcm-readable found Files
38   *          according their Patient/Study/Serie/Image characteristics
39   *          and anomymizes (NoLoad) them, creates a different name for each Patient.
40   */  
41
42 int main(int argc, char *argv[]) 
43 {
44    START_USAGE(usage)
45    " \n AnonymizeMultiPatient :\n                                             ",
46    " AnonymizeMultiPatient a full gdcm-readable Dicom image                   ",
47    "         Warning : the image is OVERWRITTEN                               ",
48    "                   to preserve image integrity, use a copy.               ",
49    " usage: AnonymizeMultiPatient dirin=inputDirectoryName                    ",
50    "       listOfElementsToRubOut : group-elem,g2-e2,... (in hexa, no space)  ",
51    "                                of extra Elements to rub out              ",
52    "       noshadowseq: user doesn't want to load Private Sequences           ",
53    "       noshadow   : user doesn't want to load Private groups (odd number) ",
54    "       noseq      : user doesn't want to load Sequences                   ",
55    "       verbose    : user wants to run the program in 'verbose mode'       ",   
56    "       debug      : user wants to run the program in 'debug mode'         ",   
57    FINISH_USAGE
58    
59
60
61    // ----- Initialize Arguments Manager ------   
62    gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
63   
64    if (argc == 1 || am->ArgMgrDefined("usage")) 
65    {
66       am->ArgMgrUsage(usage); // Display 'usage'
67       delete am;
68       return 0;
69    }
70    const char * name  = am->ArgMgrGetString("dirin");
71    if ( name == NULL )
72    {
73       delete am;
74       return 0;
75    }
76    
77    std::string dirName = name;
78  
79    bool verbose  = am->ArgMgrDefined("verbose");
80    
81    if (am->ArgMgrDefined("debug"))
82       gdcm::Debug::DebugOn();
83
84    int loadMode = gdcm::LD_ALL;
85  
86    if ( am->ArgMgrDefined("noshadowseq") )
87       loadMode |= gdcm::LD_NOSHADOWSEQ;
88    else 
89    {
90       if ( am->ArgMgrDefined("noshadow") )
91          loadMode |= gdcm::LD_NOSHADOW;
92       if ( am->ArgMgrDefined("noseq") )
93          loadMode |= gdcm::LD_NOSEQ;
94    }
95
96
97    int rubOutNb;
98    uint16_t *elemsToRubOut = am->ArgMgrGetXInt16Enum("rubout", &rubOutNb);
99  
100    // ----------- if unused Param we give up
101    if ( am->ArgMgrPrintUnusedLabels() )
102    { 
103       am->ArgMgrUsage(usage);
104       delete am;
105       return 0;
106    }
107
108    delete am;  // we don't need Argument Manager any longer
109
110  
111   // ---------------------------------------------------------- 
112   
113
114    // ----- Begin Processing -----
115
116    gdcm::DicomDir *dcmdir;
117
118    // we ask for Directory parsing
119
120    dcmdir = gdcm::DicomDir::New( );
121    dcmdir->SetLoadMode(loadMode);
122    dcmdir->SetDirectoryName(dirName);
123    dcmdir->Load();
124
125    if ( verbose )
126       std::cout << "======================= End Parsing Directory" << std::endl;
127       
128     // ----- Check the result
129     
130    if ( !dcmdir->GetFirstPatient() ) 
131    {
132       std::cout << "No patient found (?!?). Exiting."
133                 << std::endl;
134       dcmdir->Delete();
135       return 1;
136    }
137
138    gdcm::DicomDirPatient *pa;
139    gdcm::DicomDirStudy *st;
140    gdcm::DicomDirSerie *se;
141    gdcm::DicomDirVisit *vs;
142    gdcm::DicomDirImage *im;
143
144    std::string codedName; 
145    std::string fullFileName;
146    std::string patName;
147       
148    gdcm::File *f;
149   
150    pa = dcmdir->GetFirstPatient(); 
151    while ( pa )
152    {  // on degouline les PATIENT du DICOMDIR
153       patName = pa->GetEntryString(0x0010, 0x0010);
154       codedName = "g^" + gdcm::Util::ConvertToMD5(patName);
155       if (verbose)
156          std::cout << patName << " --> " << codedName << std::endl;         
157       st = pa->GetFirstStudy();
158       while ( st ) 
159       { // on degouline les STUDY de ce patient
160          se = st->GetFirstSerie();
161          while ( se ) 
162          { // on degouline les SERIES de cette study
163             im = se->GetFirstImage();
164             while ( im ) 
165             { // on degouline les Images de cette serie       
166                fullFileName = dirName;
167                fullFileName +=  gdcm::GDCM_FILESEPARATOR;
168                fullFileName += im->GetEntryString(0x0004, 0x1500);
169                if (verbose)
170                   std::cout << "FileName " << fullFileName << std::endl;
171
172                f = gdcm::File::New( );
173                f->SetLoadMode(loadMode);
174                f->SetFileName( fullFileName );
175                if ( !f->Load() )
176                  std::cout << "Load failed for [" << fullFileName << "]" << std::endl; 
177                else
178                   if (verbose)
179                      std::cout << "Load successed for [" << fullFileName << "]" << std::endl;
180
181                // 
182                //  Choose the fields to anonymize.
183                // 
184
185                // Institution name 
186                f->AddAnonymizeElement( 0x0008, 0x0080, "Xanadoo" );
187
188                // Patient's name 
189                f->AddAnonymizeElement( 0x0010, 0x0010, codedName ); 
190     
191                // Patient's ID
192                f->AddAnonymizeElement( 0x0010, 0x0020,"1515" );
193                // Patient's Birthdate
194                f->AddAnonymizeElement( 0x0010, 0x0030,"11111111" );
195                // Patient's Adress
196                f->AddAnonymizeElement( 0x0010, 0x1040,"Sing-sing" );
197                // Patient's Mother's Birth Name
198                f->AddAnonymizeElement( 0x0010, 0x1060,"g^Vampirella" );
199        
200                // Study Instance UID
201                // we may not brutaly overwrite it
202                //f->AddAnonymizeElement( 0x0020, 0x000d, "9.99.999.9999" );
203    
204                // Telephone
205                f->AddAnonymizeElement(0x0010, 0x2154, "3615" );
206
207                // deal with user defined Elements set
208
209                for (int ri=0; ri<rubOutNb; ri++)
210                {
211                   f->AddAnonymizeElement((uint32_t)elemsToRubOut[2*ri], 
212                                          (uint32_t)elemsToRubOut[2*ri+1],"*" ); 
213                }
214
215               // 
216               //      Overwrite the file
217               // 
218               // The gdcm::File remains untouched in memory    
219    
220               f->AnonymizeNoLoad();     
221
222               f->ClearAnonymizeList();
223               f->Delete();
224  
225               im = se->GetNextImage();   
226            }
227            se = st->GetNextSerie();   
228         }
229         st = pa->GetNextStudy();
230      }     
231      pa = dcmdir->GetNextPatient();    
232    }
233
234    dcmdir->Delete();        
235    return 0;
236    
237 }