1 /*=========================================================================
4 Module: $RCSfile: TestAnonymize.cxx,v $
6 Date: $Date: 2005/07/08 13:39:56 $
7 Version: $Revision: 1.2 $
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;
60 f = new gdcm::File( );
61 f->SetFileName( filename );
64 std::cout << " ... Read !" << std::endl;
65 // ============================================================
66 // Load the pixels in memory.
68 // ============================================================
70 // We need a gdcm::FileHelper, since we want to load the pixels
71 gdcm::FileHelper *fh = new gdcm::FileHelper(f);
73 // --- Don't forget to load the Pixels ...
74 // We shall not use them, but we have to load them
78 std::cout << " Image Data... Got ! " << std::endl;
81 f->AddAnonymizeElement( 0x0008, 0x0080, "Xanadoo" );
83 f->AddAnonymizeElement( 0x0010, 0x0010, "Fantomas" );
85 f->AddAnonymizeElement( 0x0010, 0x0020,"1515" );
87 f->AddAnonymizeElement( 0x0020, 0x000d, "9.99.999.9999" );
89 f->AddAnonymizeElement( 0x0010, 0x2154, "3615" );
91 std::cout << " Anonymize list... Done ! " << std::endl;
95 std::cout << " Anonymize File... Done ! " << std::endl;
97 fh->WriteDcmExplVR(output);
98 std::cout << " Anonymized File... Written ! " << std::endl;
100 f->ClearAnonymizeList();
101 std::cout << " Anonymize list... Cleared ! " << std::endl;
106 // Read the file we just wrote
107 f = new gdcm::File( output );
109 std::cout << " Anonymized File... Re-Read ! " << std::endl;
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() )
120 v = f->GetEntryValue(0x0010, 0x0010);
121 if ( v != gdcm::GDCM_UNFOUND )
122 if (v.find("Fantomas") >= v.length() )
125 if ( v != gdcm::GDCM_UNFOUND )
126 v = f->GetEntryValue(0x0010, 0x0020);
127 if (v.find("1515") >= v.length() )
130 if ( v != gdcm::GDCM_UNFOUND )
131 v = f->GetEntryValue(0x0010, 0x000d);
132 if (v.find("9.99.999.9999") >= v.length() )
143 // ============================================================
144 // Don't load the pixels in memory.
145 // Overwrite the file
146 // ============================================================
148 // Read the file we just anonymize and check
150 f = new gdcm::File( output );
152 // First, we set values to replace the ones we want to hide
155 f->AddAnonymizeElement(0x0010, 0x0010, "XXL");
156 std::cout << " replace Patient's Name " << std::endl;
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;
164 // --------------------- we overwrite the file
166 // No need to load the pixels.
167 // The gdcm::File remains untouched in memory
169 std::cout <<"Let's AnonymizeNoLoad " << std::endl;;
170 f->AnonymizeNoLoad();
172 std::cout <<"End AnonymizeNoLoad" << std::endl;
173 // No need to write the File : modif were done on disc !
176 f = new gdcm::File( output );
181 val = f->GetEntryValue(0x0010, 0x0010);
182 if ( val != gdcm::GDCM_UNFOUND )
183 if (val.find("XXL") >= v.length() )
186 if ( val != gdcm::GDCM_UNFOUND )
187 val = f->GetEntryValue(0x0010, 0x0020);
188 if (val.find("007") >= v.length() )
191 if ( val != gdcm::GDCM_UNFOUND )
192 val = f->GetEntryValue(0x0010, 0x000d);
193 if (val.find("6.66.666.6666") >= v.length() )
206 // Here we load a gdcmFile we anonymize it with and without loading the pixels,
208 int TestAnonymize(int argc, char *argv[])
210 //gdcm::Debug::DebugOn();
214 // The test is specified a specific filename, use it instead of looping
216 const std::string input = argv[1];
217 const std::string reference = argv[2];
218 return Anonymize( input, reference );
220 else if ( argc > 3 || argc == 2 )
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;
230 std::cout << " Description (Test::TestAnonymize): "
232 std::cout << " For all images in gdcmData (and not blacklisted in "
233 "Test/CMakeLists.txt)"
235 std::cout << " apply the following to each filename.xxx: "
237 std::cout << " - Create a new file "
239 std::cout << " - Anonymize the file (creates a new file) "
241 std::cout << " - Read this new file "
243 std::cout << " - Check if the written values are ok"
245 std::cout << " - AnonymizeNoLoad the new file (it s overwritten)"
247 std::cout << " - Check if the written values are ok"
249 std::cout << std::endl;
252 int retVal = 0; //by default this is an error
253 while( gdcmDataImages[i] != 0 )
255 std::string filename = GDCM_DATA_ROOT;
256 filename += "/"; //doh!
257 filename += gdcmDataImages[i];
259 std::string output = "output.dcm";
261 if( Anonymize( filename, output ) != 0 )