1 /*=========================================================================
4 Module: $RCSfile: TestAnonymize.cxx,v $
6 Date: $Date: 2005/02/09 16:41:51 $
7 Version: $Revision: 1.1 $
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.
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.
17 =========================================================================*/
19 #include "gdcmFileHelper.h"
20 #include "gdcmValEntry.h"
21 #include "gdcmBinEntry.h"
22 #include "gdcmGlobal.h"
23 #include "gdcmDebug.h"
26 #include "gdcmDataImages.h"
29 #include <unistd.h> //for access, unlink
31 #include <io.h> //for _access on Win32
35 bool FileExists(const char *filename);
36 bool RemoveFile(const char *source);
38 // ---------------------------------------------------------
40 int Anonymize(std::string const &filename,
41 std::string const &output )
43 std::cout << " Testing: " << filename << std::endl;
45 if( FileExists( output.c_str() ) )
47 if( !RemoveFile( output.c_str() ) )
49 std::cout << "Ouch, the file exist, but I cannot remove it"
55 //////////////// Step 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.
63 // ============================================================
65 // We need a gdcm::FileHelper, since we want to load the pixels
66 gdcm::FileHelper *fh1 = new gdcm::FileHelper(f1);
68 // --- Don't forget to load the Pixels ...
69 // We shall not use them, but we have to load them
73 std::cout << " Image Data... Got ! " << std::endl;
76 f1->AddAnonymizeElement( 0x0008, 0x0080, "Xanadoo" );
78 f1->AddAnonymizeElement( 0x0010, 0x0010, "Fantomas" );
80 f1->AddAnonymizeElement( 0x0010, 0x0020,"1515" );
82 f1->AddAnonymizeElement( 0x0020, 0x000d, "9.99.999.9999" );
84 f1->AddAnonymizeElement( 0x0010, 0x2154, "3615" );
86 std::cout << " Anonymize list... Done ! " << std::endl;
90 std::cout << " Anonymize File... Done ! " << std::endl;
92 fh1->WriteDcmExplVR(output);
93 std::cout << " Anonymized File... Written ! " << std::endl;
95 f1->ClearAnonymizeList();
96 std::cout << " Anonymize list... Cleared ! " << std::endl;
101 // Read the file we just wrote
102 f1 = new gdcm::File( output );
104 std::cout << " Anonymized File... Re-Read ! " << std::endl;
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() )
115 v = f1->GetEntryValue(0x0010, 0x0010);
116 if ( v != gdcm::GDCM_UNFOUND )
117 if (v.find("Fantomas") >= v.length() )
120 if ( v != gdcm::GDCM_UNFOUND )
121 v = f1->GetEntryValue(0x0010, 0x0020);
122 if (v.find("1515") >= v.length() )
125 if ( v != gdcm::GDCM_UNFOUND )
126 v = f1->GetEntryValue(0x0010, 0x000d);
127 if (v.find("9.99.999.9999") >= v.length() )
138 // ============================================================
139 // Don't load the pixels in memory.
140 // Overwrite the file
141 // ============================================================
143 // Read the file we just anonymize and check
145 f1 = new gdcm::File( output );
147 // First, we set values to replace the ones we want to hide
150 f1->AddAnonymizeElement(0x0010, 0x0010, "XXL");
151 std::cout << " replace Patient's Name " << std::endl;
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;
159 // --------------------- we overwrite the file
161 // No need to load the pixels.
162 // The gdcm::File remains untouched in memory
164 std::cout <<"Let's AnonymizeNoLoad " << std::endl;;
165 f1->AnonymizeNoLoad();
167 std::cout <<"End AnonymizeNoLoad" << std::endl;
168 // No need to write the File : modif were done on disc !
171 f1 = new gdcm::File( output );
176 val = f1->GetEntryValue(0x0010, 0x0010);
177 if ( val != gdcm::GDCM_UNFOUND )
178 if (val.find("XXL") >= v.length() )
181 if ( val != gdcm::GDCM_UNFOUND )
182 val = f1->GetEntryValue(0x0010, 0x0020);
183 if (val.find("007") >= v.length() )
186 if ( val != gdcm::GDCM_UNFOUND )
187 val = f1->GetEntryValue(0x0010, 0x000d);
188 if (val.find("6.66.666.6666") >= v.length() )
201 // Here we load a gdcmFile we anonymize it with and without loading the pixels,
203 int TestAnonymize(int argc, char *argv[])
205 //gdcm::Debug::DebugOn();
209 // The test is specified a specific filename, use it instead of looping
211 const std::string input = argv[1];
212 const std::string reference = argv[2];
213 return Anonymize( input, reference );
215 else if ( argc > 3 || argc == 2 )
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;
225 std::cout << " Description (Test::TestAnonymize): "
227 std::cout << " For all images in gdcmData (and not blacklisted in "
228 "Test/CMakeLists.txt)"
230 std::cout << " apply the following to each filename.xxx: "
232 std::cout << " - Create a new file "
234 std::cout << " - Anonymize the file (creates a new file) "
236 std::cout << " - Read this new file "
238 std::cout << " - Check if the written values are ok"
240 std::cout << " - AnonymizeNoLoad the new file (it s overwritten)"
242 std::cout << " - Check if the written values are ok"
244 std::cout << std::endl;
247 int retVal = 0; //by default this is an error
248 while( gdcmDataImages[i] != 0 )
250 std::string filename = GDCM_DATA_ROOT;
251 filename += "/"; //doh!
252 filename += gdcmDataImages[i];
254 std::string output = "output.dcm";
256 if( Anonymize( filename, output ) != 0 )