#include "gdcmFileAnonymizer.h"
#include "gdcmTesting.h"
#include "gdcmSystem.h"
#include "gdcmReader.h"
#include "gdcmFilename.h"
#include <vector>
static int TestFileAnonymize3(const char *filename, bool verbose = false)
{
using namespace gdcm;
if( verbose )
std::cout << "Processing: " << filename << std::endl;
const char subdir[] = "TestFileAnonymize3";
std::string tmpdir = Testing::GetTempDirectory( subdir );
if( !System::FileIsDirectory( tmpdir.c_str() ) )
{
System::MakeDirectory( tmpdir.c_str() );
}
std::string outfilename = Testing::GetTempFilename( filename, subdir );
if( verbose )
std::cout << "Generating: " << outfilename << std::endl;
const gdcm::Tag t1(0x0018,0x5100);
const gdcm::Tag t2(0x0018,0x1312);
const gdcm::Tag t3(0x0018,0x1313);
const gdcm::Tag t4(0x0018,0x1317);
const gdcm::Tag t7(0x0018,0x1020);
const gdcm::Tag t8(0x0004,0x1130); const gdcm::Tag t9(0x0008,0x0000); const gdcm::Tag t0(0xffff,0xffff);
std::vector<gdcm::Tag> tags;
tags.push_back( t0 );
tags.push_back( t1 );
tags.push_back( t2 );
tags.push_back( t3 );
tags.push_back( t4 );
tags.push_back( t7 );
tags.push_back( t8 );
tags.push_back( t9 );
System::RemoveFile(outfilename.c_str());
gdcm::FileAnonymizer fa;
fa.SetInputFileName( filename );
fa.SetOutputFileName( outfilename.c_str() );
for( std::vector<gdcm::Tag>::const_iterator it = tags.begin();
it != tags.end(); ++it )
{
fa.Replace( *it, "TOTO" );
}
if( !fa.Write() )
{
std::cerr << "Failed to write: " << outfilename << std::endl;
return 1;
}
gdcm::Reader r;
r.SetFileName( outfilename.c_str() );
if( !r.Read() )
{
gdcm::Filename fn( filename );
if( strcmp(fn.GetName(), "SIEMENS_MAGNETOM-12-MONO2-GDCM12-VRUN.dcm") == 0
|| strcmp(fn.GetName(), "DMCPACS_ExplicitImplicit_BogusIOP.dcm") == 0
|| strcmp(fn.GetName(), "ExplicitVRforPublicElementsImplicitVRforShadowElements.dcm") == 0 )
{
return 0;
}
std::cerr << "Failed to read: " << outfilename << std::endl;
return 1;
}
const File &f = r.GetFile();
gdcm::MediaStorage ms;
ms.SetFromFile( f );
const DataSet &ds = f.GetDataSet();
for( std::vector<gdcm::Tag>::const_iterator it = tags.begin();
it != tags.end(); ++it )
{
const gdcm::Tag & t = *it;
const bool iserror = (ms == gdcm::MediaStorage::MediaStorageDirectoryStorage && t == t8) && false; if( !ds.FindDataElement( t ) )
{
if( iserror )
{
std::cerr << "Not found element: " << t << " in " << outfilename << std::endl;
return 1;
}
}
const gdcm::DataElement & de = ds.GetDataElement( t );
if( de.GetVL() != 4 )
{
if( iserror )
{
std::cerr << "Wrong VL for: " << t << " in " << outfilename << std::endl;
return 1;
}
}
}
return 0;
}
int TestFileAnonymizer3(int argc, char *argv[])
{
if( argc == 2 )
{
const char *filename = argv[1];
return TestFileAnonymize3(filename, true);
}
gdcm::Trace::DebugOff();
gdcm::Trace::WarningOff();
gdcm::Trace::ErrorOff();
int r = 0, i = 0;
const char *filename;
const char * const *filenames = gdcm::Testing::GetFileNames();
while( (filename = filenames[i]) )
{
r += TestFileAnonymize3( filename );
++i;
}
return r;
}