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