1 /*=========================================================================
4 Module: $RCSfile: AnonymizeNoLoad.cxx,v $
6 Date: $Date: 2006/01/26 15:52:42 $
7 Version: $Revision: 1.16 $
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 =========================================================================*/
19 #include "gdcmFileHelper.h"
20 #include "gdcmCommon.h"
21 #include "gdcmDebug.h"
22 #include "gdcmDirList.h"
24 #include "gdcmArgMgr.h"
28 int main(int argc, char *argv[])
31 "\n AnonymizeNoLoad :\n ",
32 "Anonymize a gdcm-parsable Dicom image even if pixels aren't gdcm readable ",
33 " Warning : the image is OVERWRITTEN ",
34 " to preserve image integrity, use a copy. ",
35 "usage: AnonymizeNoLoad {filein=inputFileName|dirin=inputDirectoryName} ",
36 " [rubout=listOfElementsToRubOut] ",
37 " [ { [noshadowseq] | [noshadow][noseq] } ] [debug] ",
38 " inputFileName : Name of the (single) file user wants to anonymize ",
39 " inputDirectoryName : user wants to anonymize *all* the files ",
40 " within the (single Patient!) directory ",
41 " listOfElementsToRubOut : group-elem,g2-e2,... (in hexa, no space) ",
42 " of extra Elements to rub out ",
43 " noshadowseq: user doesn't want to load Private Sequences ",
44 " noshadow : user doesn't want to load Private groups (odd number) ",
45 " noseq : user doesn't want to load Sequences ",
46 " debug : user wants to run the program in 'debug mode' ",
49 // ----- Initialize Arguments Manager ------
51 gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
53 if (am->ArgMgrDefined("usage") || argc == 1)
55 am->ArgMgrUsage(usage); // Display 'usage'
60 if (am->ArgMgrDefined("debug"))
61 gdcm::Debug::DebugOn();
63 const char *fileName = am->ArgMgrGetString("filein");
64 const char *dirName = am->ArgMgrGetString("dirin");
66 if ( (fileName == 0 && dirName == 0)
68 (fileName != 0 && dirName != 0) )
71 << "Either 'filein=' or 'dirin=' must be present;"
72 << std::endl << "Not both" << std::endl;
73 am->ArgMgrUsage(usage); // Display 'usage'
78 int loadMode = gdcm::LD_ALL;
79 if ( am->ArgMgrDefined("noshadowseq") )
80 loadMode |= gdcm::LD_NOSHADOWSEQ;
83 if ( am->ArgMgrDefined("noshadow") )
84 loadMode |= gdcm::LD_NOSHADOW;
85 if ( am->ArgMgrDefined("noseq") )
86 loadMode |= gdcm::LD_NOSEQ;
90 uint16_t *elemsToRubOut = am->ArgMgrGetXInt16Enum("rubout", &rubOutNb);
92 /* if unused Param we give up */
93 if ( am->ArgMgrPrintUnusedLabels() )
95 am->ArgMgrUsage(usage);
100 delete am; // ------ we don't need Arguments Manager any longer ------
103 if ( fileName != 0 ) // ====== Deal with a single file ======
107 // Parse the input file.
109 f = gdcm::File::New( );
110 f->SetLoadMode(loadMode);
111 f->SetFileName( fileName );
113 // gdcm::File::IsReadable() is no usable here, because we deal with
114 // any kind of gdcm::Readable *document*
115 // not only gdcm::File (as opposed to gdcm::DicomDir)
118 std::cout <<std::endl
119 << "Sorry, " << fileName <<" not a gdcm-readable "
120 << "DICOM / ACR Document"
125 std::cout << fileName << " is readable " << std::endl;
128 // No need to load the pixels in memory.
129 // File will be overwritten
134 // Choose the fields to anonymize.
138 f->AddAnonymizeElement( 0x0008, 0x0080, "Xanadoo" );
140 f->AddAnonymizeElement( 0x0010, 0x0010, "g^Fantomas" );
142 f->AddAnonymizeElement( 0x0010, 0x0020,"1515" );
143 // Patient's Birthdate
144 f->AddAnonymizeElement( 0x0010, 0x0030,"11.11.1111" );
146 f->AddAnonymizeElement( 0x0010, 0x1040,"Sing-sing" );
147 // Patient's Mother's Birth Name
148 f->AddAnonymizeElement( 0x0010, 0x1060,"g^Vampirella" );
149 // Study Instance UID
150 f->AddAnonymizeElement( 0x0020, 0x000d, "9.99.999.9999" );
152 f->AddAnonymizeElement(0x0010, 0x2154, "3615" );
154 for (int ri=0; ri<rubOutNb; ri++)
156 printf("%04x,%04x\n",elemsToRubOut[2*ri], elemsToRubOut[2*ri+1]);
157 f->AddAnonymizeElement((uint32_t)elemsToRubOut[2*ri],
158 (uint32_t)elemsToRubOut[2*ri+1],"*" );
161 // Aware use will add new fields here
164 // Overwrite the file
167 std::cout <<"Let's AnonymizeNoLoad " << std::endl;
169 // The gdcm::File remains untouched in memory
171 f->AnonymizeNoLoad();
173 // No need to write the file : modif were done on disc !
174 // ( The Dicom file is overwritten )
175 std::cout <<"End AnonymizeNoLoad" << std::endl;
178 // Remove the Anonymize list
180 f->ClearAnonymizeList();
186 else // ====== Deal with a (single Patient) Directory ======
188 std::cout << "dirName [" << dirName << "]" << std::endl;
189 gdcm::DirList dirList(dirName,1); // gets recursively the file list
190 gdcm::DirListType fileList = dirList.GetFilenames();
191 for( gdcm::DirListType::iterator it = fileList.begin();
192 it != fileList.end();
195 f = gdcm::File::New( );
196 f->SetLoadMode(loadMode);
197 f->SetFileName( it->c_str() );
205 // Choose the fields to anonymize.
209 f->AddAnonymizeElement( 0x0008, 0x0080, "Xanadoo" );
211 f->AddAnonymizeElement( 0x0010, 0x0010, "g^Fantomas" );
213 f->AddAnonymizeElement( 0x0010, 0x0020,"1515" );
214 // Patient's Birthdate
215 f->AddAnonymizeElement( 0x0010, 0x0030,"11.11.1111" );
217 f->AddAnonymizeElement( 0x0010, 0x1040,"Sing-sing" );
218 // Patient's Mother's Birth Name
219 f->AddAnonymizeElement( 0x0010, 0x1060,"g^Vampirella" );
220 // Study Instance UID
221 // we may not brutaly overwrite it
222 //f->AddAnonymizeElement( 0x0020, 0x000d, "9.99.999.9999" );
224 f->AddAnonymizeElement(0x0010, 0x2154, "3615" );
226 // deal with user defined Elements set
228 for (int ri=0; ri<rubOutNb; ri++)
230 f->AddAnonymizeElement((uint32_t)elemsToRubOut[2*ri],
231 (uint32_t)elemsToRubOut[2*ri+1],"*" );
233 std::cout <<"Let's AnonymizeNoLoad " << it->c_str() << std::endl;
235 // The gdcm::File remains untouched in memory
236 // The Dicom file is overwritten on disc
238 f->AnonymizeNoLoad();
241 // Remove the Anonymize list
244 f->ClearAnonymizeList();