]> Creatis software - gdcm.git/blob - Example/AnonymizeNoLoad.cxx
Unify user interface
[gdcm.git] / Example / AnonymizeNoLoad.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: AnonymizeNoLoad.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/07/21 04:55:50 $
7   Version:   $Revision: 1.8 $
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    "                       [rubout=listOfPrivateElementsToRubOut]             ", 
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    "       listOfPrivateElementsToRubOut : group-elem,g2-e2,... (in hexa)     ",
42    "                                       of private 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'         ",
47    FINISH_USAGE
48
49    // ----- Initialize Arguments Manager ------
50   
51    gdcm::ArgMgr *am = new gdcm::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::Debug::DebugOn();
62
63    char *fileName = am->ArgMgrGetString("filein",(char *)0);
64    char *dirName  = am->ArgMgrGetString("dirin",(char *)0);
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 = 0x00000000;
79    if ( am->ArgMgrDefined("noshadowseq") )
80       loadMode |= NO_SHADOWSEQ;
81    else 
82    {
83    if ( am->ArgMgrDefined("noshadow") )
84          loadMode |= NO_SHADOW;
85       if ( am->ArgMgrDefined("noseq") )
86          loadMode |= NO_SEQ;
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    if ( fileName != 0 ) // ====== Deal with a single file ======
103    {
104
105    // 
106    //   Parse the input file.
107    // 
108       gdcm::File *f;
109       f = new gdcm::File( );
110       f->SetLoadMode(loadMode);
111       f->SetFileName( fileName );
112       bool res = f->Load();
113
114       // gdcm::File::IsReadable() is no usable here, because we deal with
115       // any kind of gdcm::Readable *document*
116       // not only gdcm::File (as opposed to gdcm::DicomDir)
117       if ( !res ) 
118       {
119           std::cout <<std::endl
120               << "Sorry, " << fileName <<"  not a gdcm-readable "
121               << "DICOM / ACR Document"
122               << std::endl;
123            delete f;
124            return 1;
125       }
126       std::cout << fileName << " is readable " << std::endl;
127
128       // 
129       //      No need to load the pixels in memory.
130       //      File will be overwritten
131       // 
132
133
134       // 
135       //  Choose the fields to anonymize.
136       // 
137
138       // Institution name 
139       f->AddAnonymizeElement( 0x0008, 0x0080, "Xanadoo" ); 
140       // Patient's name 
141       f->AddAnonymizeElement( 0x0010, 0x0010, "g^Fantomas" );      
142       // Patient's ID
143       f->AddAnonymizeElement( 0x0010, 0x0020,"1515" );
144       // Patient's Birthdate
145       f->AddAnonymizeElement( 0x0010, 0x0030,"11.11.1111" );
146       // Patient's Adress
147       f->AddAnonymizeElement( 0x0010, 0x1040,"Sing-sing" );
148       // Patient's Mother's Birth Name
149       f->AddAnonymizeElement( 0x0010, 0x1060,"g^Vampirella" );      
150       // Study Instance UID
151       f->AddAnonymizeElement( 0x0020, 0x000d, "9.99.999.9999" );
152       // Telephone
153       f->AddAnonymizeElement(0x0010, 0x2154, "3615" );
154
155       for (int ri=0; ri<rubOutNb; ri++)
156       {
157          printf("%04x,%04x\n",elemsToRubOut[2*ri], elemsToRubOut[2*ri+1]);
158          f->AddAnonymizeElement((uint32_t)elemsToRubOut[2*ri], 
159                                 (uint32_t)elemsToRubOut[2*ri+1],"*" ); 
160       }
161
162       // Aware use will add new fields here
163
164       // 
165       //      Overwrite the file
166       // 
167
168       std::cout <<"Let's AnonymizeNoLoad " << std::endl;
169
170       // The gdcm::File remains untouched in memory
171
172       f->AnonymizeNoLoad();
173
174       // No need to write the file : modif were done on disc !
175       //  ( The Dicom file is overwritten )
176       std::cout <<"End AnonymizeNoLoad" << std::endl;
177
178       // 
179       //      Remove the Anonymize list
180       //   
181       f->ClearAnonymizeList();
182  
183       delete f;
184       return 0;
185
186    }
187    else  // ====== Deal with a (single Patient) Directory ======
188    {
189       std::cout << "dirName [" << dirName << "]" << std::endl;
190       gdcm::DirList dirList(dirName,1); // gets recursively the file list
191       gdcm::DirListType fileList = dirList.GetFilenames();
192       for( gdcm::DirListType::iterator it  = fileList.begin();
193                                  it != fileList.end();
194                                  ++it )
195       {
196
197          gdcm::File *f;
198          f = new gdcm::File( );
199          f->SetLoadMode(loadMode);
200          f->SetFileName( it->c_str() );
201          bool res = f->Load();
202
203          if ( !res )
204          {
205             delete f; 
206             continue;
207          }
208          // 
209          //  Choose the fields to anonymize.
210          // 
211  
212          // Institution name 
213          f->AddAnonymizeElement( 0x0008, 0x0080, "Xanadoo" ); 
214          // Patient's name 
215          f->AddAnonymizeElement( 0x0010, 0x0010, "g^Fantomas" );   
216          // Patient's ID
217          f->AddAnonymizeElement( 0x0010, 0x0020,"1515" );
218          // Patient's Birthdate
219          f->AddAnonymizeElement( 0x0010, 0x0030,"11.11.1111" );
220          // Patient's Adress
221          f->AddAnonymizeElement( 0x0010, 0x1040,"Sing-sing" );
222          // Patient's Mother's Birth Name
223          f->AddAnonymizeElement( 0x0010, 0x1060,"g^Vampirella" );   
224          // Study Instance UID
225          // we may not brutaly overwrite it
226          //f->AddAnonymizeElement( 0x0020, 0x000d, "9.99.999.9999" );
227          // Telephone
228          f->AddAnonymizeElement(0x0010, 0x2154, "3615" );
229
230          // deal with user defined Elements set
231
232          for (int ri=0; ri<rubOutNb; ri++)
233          {
234             f->AddAnonymizeElement((uint32_t)elemsToRubOut[2*ri], 
235                                    (uint32_t)elemsToRubOut[2*ri+1],"*" ); 
236          }        
237          std::cout <<"Let's AnonymizeNoLoad " << it->c_str() << std::endl;
238
239          // The gdcm::File remains untouched in memory
240          // The Dicom file is overwritten on disc
241
242          f->AnonymizeNoLoad();
243
244          // 
245          //   Remove the Anonymize list
246          //
247
248          f->ClearAnonymizeList();
249     
250          delete f;         
251         }
252
253      }
254    return 0;
255 }
256