1 /*=========================================================================
4 Module: $RCSfile: TestAnonymize.cxx,v $
6 Date: $Date: 2005/10/18 08:35:46 $
7 Version: $Revision: 1.3 $
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 "gdcmGlobal.h"
21 #include "gdcmDebug.h"
24 #include "gdcmDataImages.h"
27 #include <unistd.h> //for access, unlink
29 #include <io.h> //for _access on Win32
33 bool FileExists(const char *filename);
34 bool RemoveFile(const char *source);
36 // ---------------------------------------------------------
38 int Anonymize(std::string const &filename,
39 std::string const &output )
41 std::cout << " Testing: " << filename << std::endl;
43 if( FileExists( output.c_str() ) )
45 if( !RemoveFile( output.c_str() ) )
47 std::cout << "Ouch, the file exist, but I cannot remove it"
53 //////////////// Step 1:
55 std::cout << std::endl;
58 f = new gdcm::File( );
59 f->SetFileName( filename );
62 std::cout << " ... Read !" << std::endl;
63 // ============================================================
64 // Load the pixels in memory.
66 // ============================================================
68 // We need a gdcm::FileHelper, since we want to load the pixels
69 gdcm::FileHelper *fh = new gdcm::FileHelper(f);
71 // --- Don't forget to load the Pixels ...
72 // We shall not use them, but we have to load them
76 std::cout << " Image Data... Got ! " << std::endl;
79 f->AddAnonymizeElement( 0x0008, 0x0080, "Xanadoo" );
81 f->AddAnonymizeElement( 0x0010, 0x0010, "Fantomas" );
83 f->AddAnonymizeElement( 0x0010, 0x0020,"1515" );
85 f->AddAnonymizeElement( 0x0020, 0x000d, "9.99.999.9999" );
87 f->AddAnonymizeElement( 0x0010, 0x2154, "3615" );
89 std::cout << " Anonymize list... Done ! " << std::endl;
93 std::cout << " Anonymize File... Done ! " << std::endl;
95 fh->WriteDcmExplVR(output);
96 std::cout << " Anonymized File... Written ! " << std::endl;
98 f->ClearAnonymizeList();
99 std::cout << " Anonymize list... Cleared ! " << std::endl;
104 // Read the file we just wrote
105 f = new gdcm::File( output );
107 std::cout << " Anonymized File... Re-Read ! " << std::endl;
112 // Compare and abort if different.
113 v = f->GetEntryValue(0x0008, 0x0080);
114 if ( v != gdcm::GDCM_UNFOUND )
115 if (v.find("Xanadoo") >= v.length() )
118 v = f->GetEntryValue(0x0010, 0x0010);
119 if ( v != gdcm::GDCM_UNFOUND )
120 if (v.find("Fantomas") >= v.length() )
123 if ( v != gdcm::GDCM_UNFOUND )
124 v = f->GetEntryValue(0x0010, 0x0020);
125 if (v.find("1515") >= v.length() )
128 if ( v != gdcm::GDCM_UNFOUND )
129 v = f->GetEntryValue(0x0010, 0x000d);
130 if (v.find("9.99.999.9999") >= v.length() )
141 // ============================================================
142 // Don't load the pixels in memory.
143 // Overwrite the file
144 // ============================================================
146 // Read the file we just anonymize and check
148 f = new gdcm::File( output );
150 // First, we set values to replace the ones we want to hide
153 f->AddAnonymizeElement(0x0010, 0x0010, "XXL");
154 std::cout << " replace Patient's Name " << std::endl;
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;
162 // --------------------- we overwrite the file
164 // No need to load the pixels.
165 // The gdcm::File remains untouched in memory
167 std::cout <<"Let's AnonymizeNoLoad " << std::endl;;
168 f->AnonymizeNoLoad();
170 std::cout <<"End AnonymizeNoLoad" << std::endl;
171 // No need to write the File : modif were done on disc !
174 f = new gdcm::File( output );
179 val = f->GetEntryValue(0x0010, 0x0010);
180 if ( val != gdcm::GDCM_UNFOUND )
181 if (val.find("XXL") >= v.length() )
184 if ( val != gdcm::GDCM_UNFOUND )
185 val = f->GetEntryValue(0x0010, 0x0020);
186 if (val.find("007") >= v.length() )
189 if ( val != gdcm::GDCM_UNFOUND )
190 val = f->GetEntryValue(0x0010, 0x000d);
191 if (val.find("6.66.666.6666") >= v.length() )
204 // Here we load a gdcmFile we anonymize it with and without loading the pixels,
206 int TestAnonymize(int argc, char *argv[])
208 //gdcm::Debug::DebugOn();
212 // The test is specified a specific filename, use it instead of looping
214 const std::string input = argv[1];
215 const std::string reference = argv[2];
216 return Anonymize( input, reference );
218 else if ( argc > 3 || argc == 2 )
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;
228 std::cout << " Description (Test::TestAnonymize): "
230 std::cout << " For all images in gdcmData (and not blacklisted in "
231 "Test/CMakeLists.txt)"
233 std::cout << " apply the following to each filename.xxx: "
235 std::cout << " - Create a new file "
237 std::cout << " - Anonymize the file (creates a new file) "
239 std::cout << " - Read this new file "
241 std::cout << " - Check if the written values are ok"
243 std::cout << " - AnonymizeNoLoad the new file (it s overwritten)"
245 std::cout << " - Check if the written values are ok"
247 std::cout << std::endl;
250 int retVal = 0; //by default this is an error
251 while( gdcmDataImages[i] != 0 )
253 std::string filename = GDCM_DATA_ROOT;
254 filename += "/"; //doh!
255 filename += gdcmDataImages[i];
257 std::string output = "output.dcm";
259 if( Anonymize( filename, output ) != 0 )