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