]> Creatis software - gdcm.git/blob - Example/AnonymizeNoLoad.cxx
COMP: Fix compile warning
[gdcm.git] / Example / AnonymizeNoLoad.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: AnonymizeNoLoad.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/07/13 13:48:56 $
7   Version:   $Revision: 1.5 $
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-readable Dicom image even if pixels aren't gdcm readable",
33    "         Warning : Warning : the image is overwritten",
34    "                   to preserve image integrity, use a copy.",
35    "usage: AnonymizeNoLoad {filein=inputFileName|dirin=inputDirectoryName}", 
36    "                       [ { [noshadowseq] | [noshadow][noseq] } ] [debug]",
37    "       inputFileName : Name of the (single) file user wants to anonymize",
38    "       inputDirectoryName : user wants to anonymize *all* the files",
39    "                            within the (single Patient!) directory",
40    "       noshadowseq: user doesn't want to load Private Sequences",
41    "       noshadow   : user doesn't want to load Private groups (odd number)",
42    "       noseq      : user doesn't want to load Sequences ",
43    "       debug      : user wants to run the program in 'debug mode' ",
44    FINISH_USAGE
45
46    // ----- Initialize Arguments Manager ------
47   
48    gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
49   
50    if (am->ArgMgrDefined("usage")) 
51    {
52       am->ArgMgrUsage(usage); // Display 'usage'
53       delete am;
54       return 0;
55    }
56
57    if (am->ArgMgrDefined("debug"))
58       gdcm::Debug::DebugOn();
59
60    char *fileName = am->ArgMgrGetString("filein",(char *)0);
61    char *dirName  = am->ArgMgrGetString("dirin",(char *)0);
62
63    if ( (fileName == 0 && dirName == 0)
64         ||
65       (fileName != 0 && dirName != 0) )
66    {
67        std::cout <<std::endl
68                  << "Either 'filein' or 'dirin' must be present;" << std::endl
69                  << "Not both" << std::endl;
70        delete am;
71        return 0;
72  }
73  
74    int loadMode = 0x00000000;
75    if ( am->ArgMgrDefined("noshadowseq") )
76       loadMode |= NO_SHADOWSEQ;
77    else 
78    {
79    if ( am->ArgMgrDefined("noshadow") )
80          loadMode |= NO_SHADOW;
81       if ( am->ArgMgrDefined("noseq") )
82          loadMode |= NO_SEQ;
83    }
84
85    delete am;  // ------ we don't need Arguments Manager any longer ------
86
87
88    if ( fileName != 0 ) // ====== Deal with a single file ======
89    {
90
91    // 
92    //   Parse the input file.
93    // 
94       gdcm::File *f;
95       f = new gdcm::File( );
96       f->SetLoadMode(loadMode);
97       f->SetFileName( fileName );
98       bool res = f->Load();
99
100       // gdcm::File::IsReadable() is no usable here, because we deal with
101       // any kind of gdcm::Readable *document*
102       // not only gdcm::File (as opposed to gdcm::DicomDir)
103       if ( !res ) 
104       {
105           std::cout <<std::endl
106               << "Sorry, " << fileName <<"  not a gdcm-readable "
107               << "DICOM / ACR Document"
108               << std::endl;
109            delete f;
110            return 1;
111       }
112       std::cout << fileName << " is readable " << std::endl;
113
114       // 
115       //      No need to load the pixels in memory.
116       //      File will be overwritten
117       // 
118
119
120       // 
121       //  Choose the fields to anonymize.
122       // 
123
124       // Institution name 
125       f->AddAnonymizeElement( 0x0008, 0x0080, "Xanadoo" ); 
126       // Patient's name 
127       f->AddAnonymizeElement( 0x0010, 0x0010, "g^Fantomas" );      
128       // Patient's ID
129       f->AddAnonymizeElement( 0x0010, 0x0020,"1515" );
130       // Patient's Birthdate
131       f->AddAnonymizeElement( 0x0010, 0x0030,"11.11.1111" );
132       // Patient's Adress
133       f->AddAnonymizeElement( 0x0010, 0x1040,"Sing-sing" );
134       // Patient's Mother's Birth Name
135       f->AddAnonymizeElement( 0x0010, 0x1060,"g^Vampirella" );      
136       // Study Instance UID
137       f->AddAnonymizeElement( 0x0020, 0x000d, "9.99.999.9999" );
138       // Telephone
139       f->AddAnonymizeElement(0x0010, 0x2154, "3615" );
140
141       // Aware use will add new fields here
142
143       // 
144       //      Overwrite the file
145       // 
146
147       std::cout <<"Let's AnonymizeNoLoad " << std::endl;
148
149       // The gdcm::File remains untouched in memory
150
151       f->AnonymizeNoLoad();
152
153       // No need to write the File : modif were done on disc !
154       // File was overwritten ...
155
156       std::cout <<"End AnonymizeNoLoad" << std::endl;
157
158       // 
159       //      Remove the Anonymize list
160       //   
161       f->ClearAnonymizeList();
162  
163       delete f;
164       return 0;
165
166    }
167    else  // ====== Deal with a (single Patient) Directory ======
168    {
169       std::cout << "dirName [" << dirName << "]" << std::endl;
170       gdcm::DirList dirList(dirName,1); // gets recursively the file list
171       gdcm::DirListType fileList = dirList.GetFilenames();
172       for( gdcm::DirListType::iterator it  = fileList.begin();
173                                  it != fileList.end();
174                                  ++it )
175       {
176
177          gdcm::File *f;
178          f = new gdcm::File( );
179          f->SetLoadMode(loadMode);
180          f->SetFileName( it->c_str() );
181          bool res = f->Load();
182
183          if ( !res )
184          {
185             delete f; 
186             continue;
187          }
188          // 
189          //  Choose the fields to anonymize.
190          // 
191  
192          // Institution name 
193          f->AddAnonymizeElement( 0x0008, 0x0080, "Xanadoo" ); 
194          // Patient's name 
195          f->AddAnonymizeElement( 0x0010, 0x0010, "g^Fantomas" );   
196          // Patient's ID
197          f->AddAnonymizeElement( 0x0010, 0x0020,"1515" );
198          // Patient's Birthdate
199          f->AddAnonymizeElement( 0x0010, 0x0030,"11.11.1111" );
200          // Patient's Adress
201          f->AddAnonymizeElement( 0x0010, 0x1040,"Sing-sing" );
202          // Patient's Mother's Birth Name
203          f->AddAnonymizeElement( 0x0010, 0x1060,"g^Vampirella" );   
204          // Study Instance UID
205          // we may not brutaly overwrite it
206          //f->AddAnonymizeElement( 0x0020, 0x000d, "9.99.999.9999" );
207          // Telephone
208          f->AddAnonymizeElement(0x0010, 0x2154, "3615" );
209         
210          std::cout <<"Let's AnonymizeNoLoad " << it->c_str() << std::endl;
211
212          // The gdcm::File remains untouched in memory
213
214          f->AnonymizeNoLoad();
215
216          // 
217          //   Remove the Anonymize list
218          //
219
220          f->ClearAnonymizeList();
221     
222          delete f;         
223         }
224
225      }
226    return 0;
227 }
228