]> Creatis software - gdcm.git/blob - Testing/TestAnonymize.cxx
Fix mistypings
[gdcm.git] / Testing / TestAnonymize.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestAnonymize.cxx,v $
5   Language:  C++
6   Date:      $Date: 2008/09/15 15:49:21 $
7   Version:   $Revision: 1.4 $
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 "gdcmGlobal.h"
21 #include "gdcmDebug.h"
22
23 //Generated file:
24 #include "gdcmDataImages.h"
25
26 #ifndef _WIN32
27 #include <unistd.h> //for access, unlink
28 #else
29 #include <io.h> //for _access on Win32
30 #endif
31
32
33 bool FileExists(const char *filename);
34 bool RemoveFile(const char *source);
35
36 // ---------------------------------------------------------
37
38 int Anonymize(std::string const &filename, 
39               std::string const &output )
40 {
41       std::cout << "   Testing: " << filename << std::endl;
42
43       if( FileExists( output.c_str() ) )
44       {
45          if( !RemoveFile( output.c_str() ) )
46          {
47             std::cout << "Ouch, the file exist, but I cannot remove it" 
48                       << std::endl;
49             return 1;
50          }
51       }
52
53       //////////////// Step 1:
54       std::cout << "      1...";
55       std::cout << std::endl;
56       
57       GDCM_NAME_SPACE::File *f;
58       f = new GDCM_NAME_SPACE::File( );
59       f->SetFileName( filename );
60       f->Load();
61       
62       std::cout << " ... Read !" << std::endl;   
63       // ============================================================
64       //   Load the pixels in memory.
65       //   Write a new file
66       // ============================================================
67
68       // We need a GDCM_NAME_SPACE::FileHelper, since we want to load the pixels        
69       GDCM_NAME_SPACE::FileHelper *fh = new GDCM_NAME_SPACE::FileHelper(f);
70
71       // --- Don't forget to load the Pixels ...
72       // We shall not use them, but we have to load them
73   
74       fh->GetImageData();
75
76       std::cout << " Image Data... Got ! " << std::endl;
77
78       // Institution name 
79       f->AddAnonymizeElement( 0x0008, 0x0080, "Xanadoo" ); 
80       // Patient's name 
81       f->AddAnonymizeElement( 0x0010, 0x0010, "Fantomas" );   
82       // Patient's ID
83       f->AddAnonymizeElement( 0x0010, 0x0020,"1515" );   
84       // Study Instance UID
85       f->AddAnonymizeElement( 0x0020, 0x000d, "9.99.999.9999" );
86       // Telephone
87       f->AddAnonymizeElement( 0x0010, 0x2154, "3615" );
88
89       std::cout << " Anonymize list... Done ! " << std::endl;
90
91       f->AnonymizeFile();
92
93       std::cout << " Anonymize File... Done ! " << std::endl;
94  
95       fh->WriteDcmExplVR(output);  
96       std::cout << " Anonymized File... Written ! " << std::endl;
97
98       f->ClearAnonymizeList();
99       std::cout << " Anonymize list... Cleared ! " << std::endl;
100     
101       delete f;
102       delete fh;
103
104       // Read the file we just wrote
105       f = new GDCM_NAME_SPACE::File( output );
106
107       std::cout << " Anonymized File... Re-Read ! " << std::endl;
108
109       std::string v;
110       bool plouf = false;
111
112       // Compare and abort if different.
113       v = f->GetEntryValue(0x0008, 0x0080);
114       if ( v != GDCM_NAME_SPACE::GDCM_UNFOUND ) 
115          if (v.find("Xanadoo") >= v.length() )
116             plouf = true;
117
118       v = f->GetEntryValue(0x0010, 0x0010);
119       if ( v != GDCM_NAME_SPACE::GDCM_UNFOUND ) 
120          if (v.find("Fantomas") >= v.length() )
121             plouf = true;
122
123       if ( v != GDCM_NAME_SPACE::GDCM_UNFOUND )
124          v = f->GetEntryValue(0x0010, 0x0020);
125          if (v.find("1515") >= v.length() )
126             plouf = true;
127
128        if ( v != GDCM_NAME_SPACE::GDCM_UNFOUND )
129          v = f->GetEntryValue(0x0010, 0x000d);
130          if (v.find("9.99.999.9999") >= v.length() )
131             plouf = true;
132       
133       delete f;
134
135       if ( !plouf)
136       { 
137          return 1;
138       } 
139
140  
141    // ============================================================
142    //   Don't load the pixels in memory.
143    //   Overwrite the file
144    // ============================================================
145
146       // Read the file we just anonymize and check
147
148       f = new GDCM_NAME_SPACE::File( output );
149
150       // First, we set values to replace the ones we want to hide
151    
152       // Patient's name 
153       f->AddAnonymizeElement(0x0010, 0x0010, "XXL");  
154       std::cout << " replace Patient's Name " << std::endl;
155       // Patient's ID
156       f->AddAnonymizeElement( 0x0010, 0x0020,"007" );
157       std::cout << " replace Patient's ID " << std::endl;
158       // Study Instance UID
159       f->AddAnonymizeElement(0x0020, 0x000d, "6.66.666.6666" );
160       std::cout << " replace Study ID " << std::endl;
161
162       // --------------------- we overwrite the file
163
164       // No need to load the pixels.
165       // The GDCM_NAME_SPACE::File remains untouched in memory
166
167       std::cout <<"Let's AnonymizeNoLoad " << std::endl;;
168       f->AnonymizeNoLoad();
169
170       std::cout <<"End AnonymizeNoLoad" << std::endl;
171       // No need to write the File : modif were done on disc !
172  
173       delete f;
174       f = new GDCM_NAME_SPACE::File( output );
175
176       std::string val;
177       plouf = false;
178
179       val = f->GetEntryValue(0x0010, 0x0010);
180       if ( val != GDCM_NAME_SPACE::GDCM_UNFOUND ) 
181          if (val.find("XXL") >= v.length() )
182             plouf = true;
183      
184       if ( val != GDCM_NAME_SPACE::GDCM_UNFOUND )
185          val = f->GetEntryValue(0x0010, 0x0020);
186          if (val.find("007") >= v.length() )
187       plouf = true;
188
189       if ( val != GDCM_NAME_SPACE::GDCM_UNFOUND )
190          val = f->GetEntryValue(0x0010, 0x000d);
191          if (val.find("6.66.666.6666") >= v.length() )
192       plouf = true;
193       
194       delete f;
195
196       if ( !plouf)
197       { 
198          return 1;
199       }                       
200
201       return 0; // no fail
202 }
203
204 // Here we load a gdcmFile we anonymize it with and without loading the pixels,
205
206 int TestAnonymize(int argc, char *argv[])
207 {
208    //GDCM_NAME_SPACE::Debug::DebugOn();
209
210    if ( argc == 3 )
211    {
212       // The test is specified a specific filename, use it instead of looping
213       // over all images
214       const std::string input = argv[1];
215       const std::string reference = argv[2];
216       return Anonymize( input, reference );
217    }
218    else if ( argc > 3 || argc == 2 )
219    {
220       std::cout << "   Usage: " << argv[0]
221                 << " (no arguments needed)." << std::endl;
222       std::cout << "or   Usage: " << argv[0]
223                 << " filename.dcm reference.dcm" << std::endl;
224       return 1;
225    }
226    // else other cases:
227
228    std::cout << "   Description (Test::TestAnonymize): "
229              << std::endl;
230    std::cout << "   For all images in gdcmData (and not blacklisted in "
231                 "Test/CMakeLists.txt)"
232              << std::endl;
233    std::cout << "   apply the following to each filename.xxx: "
234              << std::endl;
235    std::cout << "   - Create a new file " 
236              << std::endl;
237    std::cout << "   - Anonymize the file (creates a new file) " 
238              << std::endl;
239    std::cout << "   - Read this new file " 
240              << std::endl;
241    std::cout << "   - Check if the written values are ok" 
242              << std::endl;
243    std::cout << "   - AnonymizeNoLoad the new file (it s overwritten)" 
244              << std::endl;
245    std::cout << "   - Check if the written values are ok" 
246              << std::endl;
247    std::cout << std::endl;
248
249    int i =0;
250    int retVal = 0;  //by default this is an error
251    while( gdcmDataImages[i] != 0 )
252    {
253       std::string filename = GDCM_DATA_ROOT;
254       filename += "/";  //doh!
255       filename += gdcmDataImages[i];
256
257       std::string output = "output.dcm";
258
259       if( Anonymize( filename, output ) != 0 )
260       {
261          retVal++;
262       }
263       i++;
264    }
265    return retVal;
266 }