]> Creatis software - gdcm.git/blob - Example/AnonymizeNoLoad.cxx
6ffea195f9019c58e35a15ae63d51d1b8ffddb66
[gdcm.git] / Example / AnonymizeNoLoad.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: AnonymizeNoLoad.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/08/28 17:26:31 $
7   Version:   $Revision: 1.10 $
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=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'         ",
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    gdcm::File *f;
103    if ( fileName != 0 ) // ====== Deal with a single file ======
104    {
105
106    // 
107    //   Parse the input file.
108    // 
109       f = new gdcm::File( );
110       f->SetLoadMode(loadMode);
111       f->SetFileName( fileName );
112
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)
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            delete f;
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       // 
134       //  Choose the fields to anonymize.
135       // 
136
137       // Institution name 
138       f->AddAnonymizeElement( 0x0008, 0x0080, "Xanadoo" ); 
139       // Patient's name 
140       f->AddAnonymizeElement( 0x0010, 0x0010, "g^Fantomas" );      
141       // Patient's ID
142       f->AddAnonymizeElement( 0x0010, 0x0020,"1515" );
143       // Patient's Birthdate
144       f->AddAnonymizeElement( 0x0010, 0x0030,"11.11.1111" );
145       // Patient's Adress
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" );
151       // Telephone
152       f->AddAnonymizeElement(0x0010, 0x2154, "3615" );
153
154       for (int ri=0; ri<rubOutNb; ri++)
155       {
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],"*" ); 
159       }
160
161       // Aware use will add new fields here
162
163       // 
164       //      Overwrite the file
165       // 
166
167       std::cout <<"Let's AnonymizeNoLoad " << std::endl;
168
169       // The gdcm::File remains untouched in memory
170
171       f->AnonymizeNoLoad();
172
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;
176
177       // 
178       //      Remove the Anonymize list
179       //   
180       f->ClearAnonymizeList();
181  
182       delete f;
183       return 0;
184
185    }
186    else  // ====== Deal with a (single Patient) Directory ======
187    {
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();
193                                  ++it )
194       {
195          f = new gdcm::File( );
196          f->SetLoadMode(loadMode);
197          f->SetFileName( it->c_str() );
198
199          if ( !f->Load() )
200          {
201             delete f; 
202             continue;
203          }
204          // 
205          //  Choose the fields to anonymize.
206          // 
207  
208          // Institution name 
209          f->AddAnonymizeElement( 0x0008, 0x0080, "Xanadoo" ); 
210          // Patient's name 
211          f->AddAnonymizeElement( 0x0010, 0x0010, "g^Fantomas" );   
212          // Patient's ID
213          f->AddAnonymizeElement( 0x0010, 0x0020,"1515" );
214          // Patient's Birthdate
215          f->AddAnonymizeElement( 0x0010, 0x0030,"11.11.1111" );
216          // Patient's Adress
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" );
223          // Telephone
224          f->AddAnonymizeElement(0x0010, 0x2154, "3615" );
225
226          // deal with user defined Elements set
227
228          for (int ri=0; ri<rubOutNb; ri++)
229          {
230             f->AddAnonymizeElement((uint32_t)elemsToRubOut[2*ri], 
231                                    (uint32_t)elemsToRubOut[2*ri+1],"*" ); 
232          }        
233          std::cout <<"Let's AnonymizeNoLoad " << it->c_str() << std::endl;
234
235          // The gdcm::File remains untouched in memory
236          // The Dicom file is overwritten on disc
237
238          f->AnonymizeNoLoad();
239
240          // 
241          //   Remove the Anonymize list
242          //
243
244          f->ClearAnonymizeList();
245     
246          delete f;         
247         }
248
249      }
250    return 0;
251 }
252