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