1 /*=========================================================================
4 Module: $RCSfile: AnonymizeMultiPatient.cxx,v $
6 Date: $Date: 2007/11/13 11:49:09 $
7 Version: $Revision: 1.7 $
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.
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.
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"
29 #include "gdcmFileHelper.h"
31 #include "gdcmArgMgr.h"
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.
42 int main(int argc, char *argv[])
45 " \n AnonymizeMultiPatient :\n ",
46 " AnonymizeMultiPatient a full gdcm-readable Dicom image ",
47 " optionnaly, creates the DICOMDIR ",
48 " Warning : the image is OVERWRITTEN ",
49 " to preserve image integrity, use a copy. ",
50 " usage: AnonymizeMultiPatient dirin=inputDirectoryName dicomdir ",
51 " listOfElementsToRubOut : group-elem,g2-e2,... (in hexa, no space) ",
52 " of extra Elements to rub out ",
53 " dicomdir : user wants to generate a DICOMDIR ",
54 " noshadowseq: user doesn't want to load Private Sequences ",
55 " noshadow : user doesn't want to load Private groups (odd number) ",
56 " noseq : user doesn't want to load Sequences ",
57 " verbose : user wants to run the program in 'verbose mode' ",
58 " debug : user wants to run the program in 'debug mode' ",
63 // ----- Initialize Arguments Manager ------
64 GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
66 if (argc == 1 || am->ArgMgrDefined("usage"))
68 am->ArgMgrUsage(usage); // Display 'usage'
72 const char * name = am->ArgMgrGetString("dirin");
79 std::string dirName = name;
81 bool verbose = ( 0 != am->ArgMgrDefined("verbose") );
82 bool dicomdir = ( 0 != am->ArgMgrDefined("dicomdir") );
84 if (am->ArgMgrDefined("debug"))
85 GDCM_NAME_SPACE::Debug::DebugOn();
87 int loadMode = GDCM_NAME_SPACE::LD_ALL;
89 if ( am->ArgMgrDefined("noshadowseq") )
90 loadMode |= GDCM_NAME_SPACE::LD_NOSHADOWSEQ;
93 if ( am->ArgMgrDefined("noshadow") )
94 loadMode |= GDCM_NAME_SPACE::LD_NOSHADOW;
95 if ( am->ArgMgrDefined("noseq") )
96 loadMode |= GDCM_NAME_SPACE::LD_NOSEQ;
101 uint16_t *elemsToRubOut = am->ArgMgrGetXInt16Enum("rubout", &rubOutNb);
103 // ----------- if unused Param we give up
104 if ( am->ArgMgrPrintUnusedLabels() )
106 am->ArgMgrUsage(usage);
111 delete am; // we don't need Argument Manager any longer
114 // ----------------------------------------------------------
117 // ----- Begin Processing -----
119 GDCM_NAME_SPACE::DicomDir *dcmdir;
121 // we ask for Directory parsing
123 dcmdir = GDCM_NAME_SPACE::DicomDir::New( );
124 dcmdir->SetLoadMode(loadMode);
125 dcmdir->SetDirectoryName(dirName);
129 std::cout << "======================= End Parsing Directory" << std::endl;
131 // ----- Check the result
133 if ( !dcmdir->GetFirstPatient() )
135 std::cout << "No patient found (?!?). Exiting."
141 GDCM_NAME_SPACE::DicomDirPatient *pa;
142 GDCM_NAME_SPACE::DicomDirStudy *st;
143 GDCM_NAME_SPACE::DicomDirSerie *se;
144 GDCM_NAME_SPACE::DicomDirImage *im;
146 std::string codedName;
148 std::string fullFileName;
152 GDCM_NAME_SPACE::File *f;
154 pa = dcmdir->GetFirstPatient();
156 { // on degouline les PATIENT du DICOMDIR
157 patName = pa->GetEntryString(0x0010, 0x0010);
158 codedName = "g^" + GDCM_NAME_SPACE::Util::ConvertToMD5(patName);
159 patID = pa->GetEntryString(0x0010, 0x0020);
160 codedID = GDCM_NAME_SPACE::Util::ConvertToMD5(patID);
163 std::cout << "[" << patName << "] --> [" << codedName << "]" << std::endl;
164 std::cout << "[" << patID << "] --> [" << codedID << "]" << std::endl;
166 st = pa->GetFirstStudy();
168 { // on degouline les STUDY de ce patient
169 se = st->GetFirstSerie();
171 { // on degouline les SERIES de cette study
172 im = se->GetFirstImage();
174 { // on degouline les Images de cette serie
175 fullFileName = dirName;
176 fullFileName += GDCM_NAME_SPACE::GDCM_FILESEPARATOR;
177 fullFileName += im->GetEntryString(0x0004, 0x1500);
179 std::cout << "FileName " << fullFileName << std::endl;
181 f = GDCM_NAME_SPACE::File::New( );
182 f->SetLoadMode(loadMode);
183 f->SetFileName( fullFileName );
185 std::cout << "Load failed for [" << fullFileName << "]" << std::endl;
188 std::cout << "Load successed for [" << fullFileName << "]" << std::endl;
191 // Choose the fields to anonymize.
195 f->AddAnonymizeElement( 0x0008, 0x0080, "Xanadoo" );
198 f->AddAnonymizeElement( 0x0010, 0x0010, codedName );
201 //f->AddAnonymizeElement( 0x0010, 0x0020,"1515" );
202 f->AddAnonymizeElement( 0x0010, 0x0020,codedID );
203 // Patient's Birthdate
204 f->AddAnonymizeElement( 0x0010, 0x0030,"11111111" );
206 f->AddAnonymizeElement( 0x0010, 0x1040,"Sing-sing" );
207 // Patient's Mother's Birth Name
208 f->AddAnonymizeElement( 0x0010, 0x1060,"g^Vampirella" );
210 // Study Instance UID
211 // we may not brutaly overwrite it
212 //f->AddAnonymizeElement( 0x0020, 0x000d, "9.99.999.9999" );
215 f->AddAnonymizeElement(0x0010, 0x2154, "3615" );
217 // deal with user defined Elements set
219 for (int ri=0; ri<rubOutNb; ri++)
221 f->AddAnonymizeElement((uint32_t)elemsToRubOut[2*ri],
222 (uint32_t)elemsToRubOut[2*ri+1],"*" );
226 // Overwrite the file
228 // The GDCM_NAME_SPACE::File remains untouched in memory
230 f->AnonymizeNoLoad();
232 f->ClearAnonymizeList();
235 im = se->GetNextImage();
237 se = st->GetNextSerie();
239 st = pa->GetNextStudy();
241 pa = dcmdir->GetNextPatient();
248 std::cout << "DICOMDIR creation in progress ..." << std::endl;
249 dcmdir = GDCM_NAME_SPACE::DicomDir::New( );
250 dcmdir->SetLoadMode(loadMode);
251 dcmdir->SetDirectoryName(dirName);
253 dcmdir->Write("DICOMDIR");