]> Creatis software - gdcm.git/blob - Example/AnonymizeNoLoad.cxx
Fix mistypings
[gdcm.git] / Example / AnonymizeNoLoad.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: AnonymizeNoLoad.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/06/06 13:03:57 $
7   Version:   $Revision: 1.21 $
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 "gdcmFile.h"
19 #include "gdcmFileHelper.h"
20 #include "gdcmCommon.h"
21 #include "gdcmDebug.h"
22 #include "gdcmDirList.h"
23
24 #include "gdcmArgMgr.h"
25
26 #include <iostream>
27
28 int main(int argc, char *argv[])
29 {
30    START_USAGE(usage)
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      : developper wants to run the program in 'debug mode'   ",
47    FINISH_USAGE
48
49    // ----- Initialize Arguments Manager ------
50   
51    GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
52   
53    if (am->ArgMgrDefined("usage") || argc == 1) 
54    {
55       am->ArgMgrUsage(usage); // Display 'usage'
56       delete am;
57       return 0;
58    }
59
60    if (am->ArgMgrDefined("debug"))
61       GDCM_NAME_SPACE::Debug::DebugOn();
62
63    const char *fileName = am->ArgMgrGetString("filein");
64    const char *dirName  = am->ArgMgrGetString("dirin");
65
66    if ( (fileName == 0 && dirName == 0)
67         ||
68         (fileName != 0 && dirName != 0) )
69    {
70        std::cout <<std::endl
71                  << "Either 'filein=' or 'dirin=' must be present;" 
72                  << std::endl << "Not both" << std::endl;
73        am->ArgMgrUsage(usage); // Display 'usage'  
74        delete am;
75        return 0;
76  }
77  
78    int loadMode = GDCM_NAME_SPACE::LD_ALL;
79    if ( am->ArgMgrDefined("noshadowseq") )
80       loadMode |= GDCM_NAME_SPACE::LD_NOSHADOWSEQ;
81    else 
82    {
83       if ( am->ArgMgrDefined("noshadow") )
84          loadMode |= GDCM_NAME_SPACE::LD_NOSHADOW;
85       if ( am->ArgMgrDefined("noseq") )
86          loadMode |= GDCM_NAME_SPACE::LD_NOSEQ;
87    }
88
89    int rubOutNb;
90    uint16_t *elemsToRubOut = am->ArgMgrGetXInt16Enum("rubout", &rubOutNb);
91
92    /* if unused Param we give up */
93    if ( am->ArgMgrPrintUnusedLabels() )
94    {
95       am->ArgMgrUsage(usage);
96       delete am;
97       return 0;
98    } 
99  
100    delete am;  // ------ we don't need Arguments Manager any longer ------
101
102    GDCM_NAME_SPACE::File *f;
103    if ( fileName != 0 ) // ====== Deal with a single file ======
104    {
105
106    // 
107    //   Parse the input file.
108    // 
109       f = GDCM_NAME_SPACE::File::New( );
110       f->SetLoadMode(loadMode);
111       f->SetFileName( fileName );
112
113       // GDCM_NAME_SPACE::File::IsReadable() is no usable here, because we deal with
114       // any kind of GDCM_NAME_SPACE::Readable *document*
115       // not only GDCM_NAME_SPACE::File (as opposed to GDCM_NAME_SPACE::DicomDir)
116       if ( !f->Load() ) 
117       {
118           std::cout <<std::endl
119               << "Sorry, " << fileName <<"  not a gdcm-readable "
120               << "DICOM / ACR Document"
121               << std::endl;
122            f->Delete();
123            return 1;
124       }
125       std::cout << fileName << " is readable " << std::endl;
126
127       // 
128       //      No need to load the pixels in memory.
129       //      File will be overwritten
130       // 
131
132       // 
133       //  Choose the fields to anonymize.
134       // 
135
136       // Institution name 
137       f->AddAnonymizeElement( 0x0008, 0x0080, "gdcm-Xanadoo Hospital" ); 
138       // Patient's name 
139       f->AddAnonymizeElement( 0x0010, 0x0010, "gdcm^Fantomas" );      
140       // Patient's ID
141       f->AddAnonymizeElement( 0x0010, 0x0020,"1515" );
142       // Patient's Birthdate
143       f->AddAnonymizeElement( 0x0010, 0x0030,"11111111" );
144       // Patient's Adress
145       f->AddAnonymizeElement( 0x0010, 0x1040,"gdcm at Sing-sing.org" );
146       // Patient's Mother's Birth Name
147       f->AddAnonymizeElement( 0x0010, 0x1060,"gdcm^Vampirella" );      
148       // Study Instance UID
149       f->AddAnonymizeElement( 0x0020, 0x000d, "9.99.999.9999" );
150       // Telephone
151       f->AddAnonymizeElement(0x0010, 0x2154, "3615" );
152
153       for (int ri=0; ri<rubOutNb; ri++)
154       {
155          printf("%04x,%04x\n",elemsToRubOut[2*ri], elemsToRubOut[2*ri+1]);
156          f->AddAnonymizeElement((uint32_t)elemsToRubOut[2*ri], 
157                                 (uint32_t)elemsToRubOut[2*ri+1],"*" ); 
158       }
159
160       // Aware use will add new fields here
161
162       // 
163       //      Overwrite the file
164       // 
165
166       std::cout <<"Let's AnonymizeNoLoad " << std::endl;
167
168       // The GDCM_NAME_SPACE::File remains untouched in memory
169
170       f->AnonymizeNoLoad();
171
172       // No need to write the file : modif were done on disc !
173       //  ( The Dicom file is overwritten )
174       std::cout <<"End AnonymizeNoLoad" << std::endl;
175
176       // 
177       //      Remove the Anonymize list
178       //   
179       f->ClearAnonymizeList();
180  
181       f->Delete();
182       return 0;
183
184    }
185    else  // ====== Deal with a (single Patient) Directory ======
186    {
187       std::cout << "dirName [" << dirName << "]" << std::endl;
188       GDCM_NAME_SPACE::DirList dirList(dirName,1); // gets recursively the file list
189       GDCM_NAME_SPACE::DirListType fileList = dirList.GetFilenames();
190       for( GDCM_NAME_SPACE::DirListType::iterator it  = fileList.begin();
191                                  it != fileList.end();
192                                  ++it )
193       {
194          f = GDCM_NAME_SPACE::File::New( );
195          f->SetLoadMode(loadMode);
196          f->SetFileName( it->c_str() );
197
198          if ( !f->Load() )
199          {
200             f->Delete();
201             continue;
202          }
203          // 
204          //  Choose the fields to anonymize.
205          // 
206  
207          // Institution name 
208          f->AddAnonymizeElement( 0x0008, 0x0080, "gdcm-Xanadoo Hospital" ); 
209          // Patient's name 
210          f->AddAnonymizeElement( 0x0010, 0x0010, "gdcm^Fantomas" );   
211          // Patient's ID
212          f->AddAnonymizeElement( 0x0010, 0x0020,"1515" );
213          // Patient's Birthdate
214          f->AddAnonymizeElement( 0x0010, 0x0030,"11111111" );
215          // Patient's Adress
216          f->AddAnonymizeElement( 0x0010, 0x1040,"gdcm at Sing-sing.org" );
217          // Patient's Mother's Birth Name
218          f->AddAnonymizeElement( 0x0010, 0x1060,"gdcm^Vampirella" );   
219          // Study Instance UID
220          // we may not brutaly overwrite it
221          //f->AddAnonymizeElement( 0x0020, 0x000d, "9.99.999.9999" );
222          // Telephone
223          f->AddAnonymizeElement(0x0010, 0x2154, "3615" );
224
225          // deal with user defined Elements set
226
227          for (int ri=0; ri<rubOutNb; ri++)
228          {
229             f->AddAnonymizeElement((uint32_t)elemsToRubOut[2*ri], 
230                                    (uint32_t)elemsToRubOut[2*ri+1],"*" ); 
231          }        
232          std::cout <<"Let's AnonymizeNoLoad " << it->c_str() << std::endl;
233
234          // The GDCM_NAME_SPACE::File remains untouched in memory
235          // The Dicom file is overwritten on disc
236
237          f->AnonymizeNoLoad();
238
239          // 
240          //   Remove the Anonymize list
241          //
242
243          f->ClearAnonymizeList();
244     
245          f->Delete();
246       }
247    }
248    return 0;
249 }
250