GDCM: GenFakeIdentifyFile.cxx

GDCM 3.2.5

GenFakeIdentifyFile.cxx
/*=========================================================================
Program: GDCM (Grassroots DICOM). A DICOM library
Copyright (c) 2006-2011 Mathieu Malaterre
All rights reserved.
See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "gdcmReader.h"
#include "gdcmGlobal.h"
#include "gdcmDummyValueGenerator.h"
#include "gdcmMediaStorage.h"
#include "gdcmWriter.h"
#include "gdcmItem.h"
#include "gdcmImageReader.h"
#include "gdcmSequenceOfItems.h"
#include "gdcmAttribute.h"
#include "gdcmFile.h"
#include "gdcmTag.h"
#include "gdcmDict.h"
#include "gdcmDictEntry.h"
#include "gdcmDicts.h"
#include "gdcmTransferSyntax.h"
#include "gdcmUIDGenerator.h"
#include "gdcmAnonymizer.h"
#include <cstdlib>
#include <cstring>
gdcm::DataElement CreateFakeElement(gdcm::Tag const &tag, bool toremove)
{
static const gdcm::Global &g = gdcm::Global::GetInstance();
static const gdcm::Dicts &dicts = g. GetDicts();
static const gdcm::Dict &pubdict = dicts. GetPublicDict();
static size_t countglobal = 0;
static std::vector<gdcm::Tag> balcptags =
gdcm::Anonymizer::GetBasicApplicationLevelConfidentialityProfileAttributes();
size_t count = countglobal % balcptags.size();
const gdcm::DictEntry &dictentry = pubdict. GetDictEntry(tag);
gdcm::DataElement de;
de. SetTag( tag );
using gdcm::VR;
const VR &vr = dictentry. GetVR();
//if( vr != VR::INVALID )
if( vr.IsDual() )
{
if( vr == VR::US_SS )
{
de. SetVR( VR::US );
}
else if( vr == VR::US_SS_OW )
{
de.SetVR( VR::OW );
}
else if( vr == VR::OB_OW )
{
de.SetVR( VR::OB );
}
}
else
{
de.SetVR( vr );
}
const char str[] = "BasicApplicationLevelConfidentialityProfileAttributes";
const char safe[] = "This is safe to keep";
if( de. GetVR() != VR::SQ )
{
if( toremove )
de. SetByteValue( str, (uint32_t)strlen(str) );
else
de.SetByteValue( safe, (uint32_t)strlen(safe) );
}
else
{
// Create an item
gdcm::Item it;
it. SetVLToUndefined();
gdcm::DataSet &nds = it. GetNestedDataSet();
// Insert sequence into data set
gdcm_assert(de.GetVR() == gdcm::VR::SQ );
gdcm::SmartPointer<gdcm::SequenceOfItems> sq = new gdcm::SequenceOfItems();
sq->SetLengthToUndefined();
de. SetValue(*sq);
de.SetVLToUndefined();
//ds.Insert(de);
if( !toremove )
{
nds. Insert( CreateFakeElement( balcptags[count], true ) );
countglobal++;
}
else
{
gdcm::Attribute<0x0008,0x0000> at1 = { 0 }; // This element has no reason to be 'anonymized'...
nds.Insert( at1. GetAsDataElement() );
gdcm::Attribute<0x000a,0x0000> at2 = { 0 };
nds.Insert( at2.GetAsDataElement() );
}
sq->AddItem(it);
}
return de;
}
/*
*/
int main(int argc, char *argv[])
{
if( argc < 2 )
{
std::cerr << argv[0] << " output.dcm" << std::endl;
return 1;
}
using gdcm::Tag;
using gdcm::VR;
const char *outfilename = argv[1];
std::vector<gdcm::Tag> balcptags =
gdcm::Anonymizer::GetBasicApplicationLevelConfidentialityProfileAttributes();
gdcm::Writer w;
gdcm::File &f = w. GetFile();
gdcm::DataSet &ds = f. GetDataSet();
// Add attribute that need to be anonymized:
std::vector<gdcm::Tag>::const_iterator it = balcptags.begin();
for(; it != balcptags.end(); ++it)
{
ds.Insert( CreateFakeElement( *it, true ) );
}
// Add attribute that do NOT need to be anonymized:
static const gdcm::Global &g = gdcm::Global::GetInstance();
static const gdcm::Dicts &dicts = g.GetDicts();
static const gdcm::Dict &pubdict = dicts.GetPublicDict();
using gdcm::Dict;
Dict::ConstIterator dictit = pubdict. Begin();
for(; dictit != pubdict. End(); ++dictit)
{
const gdcm::Tag &dicttag = dictit->first;
if( dicttag == Tag(0x6e65,0x6146) ) break;
//const gdcm::DictEntry &dictentry = dictit->second;
ds.Insert( CreateFakeElement( dicttag, false ) );
}
ds. Remove( gdcm::Tag(0x400,0x500) );
ds.Remove( gdcm::Tag(0x12,0x62) );
ds.Remove( gdcm::Tag(0x12,0x63) );
// Make sure to override any UID stuff
gdcm::UIDGenerator uid;
gdcm::DataElement de( Tag(0x8,0x18) ); // SOP Instance UID
de.SetVR( VR::UI );
const char *u = uid. Generate();
de.SetByteValue( u, (uint32_t)strlen(u) );
//ds.Insert( de );
ds. Replace( de );
de.SetTag( Tag(0x8,0x16) ); // SOP Class UID
de.SetVR( VR::UI );
gdcm::MediaStorage ms( gdcm::MediaStorage::RawDataStorage );
de.SetByteValue( ms.GetString(), (uint32_t)strlen(ms.GetString()));
ds.Replace( de ); // replace !
gdcm::FileMetaInformation &fmi = f. GetHeader();
//fmi.SetDataSetTransferSyntax( gdcm::TransferSyntax::ImplicitVRLittleEndian );
fmi. SetDataSetTransferSyntax( gdcm::TransferSyntax::ExplicitVRLittleEndian );
w. SetCheckFileMetaInformation( true );
w. SetFileName( outfilename );
if (!w. Write() )
{
return 1;
}
return 0;
}
gdcm::Anonymizer::GetBasicApplicationLevelConfidentialityProfileAttributes
static std::vector< Tag > GetBasicApplicationLevelConfidentialityProfileAttributes()
Return the list of Tag that will be considered when anonymizing a DICOM file.
gdcm::Attribute
Attribute class This class use template metaprograming tricks to let the user know when the template ...
Definition gdcmAttribute.h:86
gdcm::Attribute::GetAsDataElement
DataElement GetAsDataElement() const
Definition gdcmAttribute.h:183
gdcm::DataElement
Class to represent a Data Element either Implicit or Explicit.
Definition gdcmDataElement.h:59
gdcm::DataElement::GetVR
VR const & GetVR() const
Definition gdcmDataElement.h:84
gdcm::DataElement::SetTag
void SetTag(const Tag &t)
Definition gdcmDataElement.h:71
gdcm::DataElement::SetVLToUndefined
void SetVLToUndefined()
gdcm::DataElement::SetValue
void SetValue(Value const &vl)
Definition gdcmDataElement.h:100
gdcm::DataElement::SetByteValue
void SetByteValue(const char *array, VL length)
Definition gdcmDataElement.h:126
gdcm::DataElement::SetVR
void SetVR(VR const &vr)
Definition gdcmDataElement.h:88
gdcm::DataSet
Class to represent a Data Set (which contains Data Elements).
Definition gdcmDataSet.h:56
gdcm::DataSet::Remove
SizeType Remove(const Tag &tag)
Completely remove a dataelement from the dataset.
Definition gdcmDataSet.h:172
gdcm::DataSet::Insert
void Insert(const DataElement &de)
Definition gdcmDataSet.h:128
gdcm::DataSet::Replace
void Replace(const DataElement &de)
Replace a dataelement with another one.
Definition gdcmDataSet.h:150
gdcm::DictEntry
Class to represent an Entry in the Dict.
Definition gdcmDictEntry.h:37
gdcm::DictEntry::GetVR
const VR & GetVR() const
Set/Get VR.
Definition gdcmDictEntry.h:53
gdcm::Dict
Class to represent a map of DictEntry.
Definition gdcmDict.h:45
gdcm::Dict::End
ConstIterator End() const
Definition gdcmDict.h:62
gdcm::Dict::GetDictEntry
const DictEntry & GetDictEntry(const Tag &tag) const
Definition gdcmDict.h:77
gdcm::Dict::Begin
ConstIterator Begin() const
Definition gdcmDict.h:61
gdcm::Dicts
Class to manipulate the sum of knowledge (all the dict user load).
Definition gdcmDicts.h:29
gdcm::Dicts::GetPublicDict
const Dict & GetPublicDict() const
gdcm::FileMetaInformation
Class to represent a File Meta Information.
Definition gdcmFileMetaInformation.h:41
gdcm::FileMetaInformation::SetDataSetTransferSyntax
void SetDataSetTransferSyntax(const TransferSyntax &ts)
gdcm::File
a DICOM File
Definition gdcmFile.h:34
gdcm::File::GetDataSet
const DataSet & GetDataSet() const
Get Data Set.
Definition gdcmFile.h:57
gdcm::File::GetHeader
const FileMetaInformation & GetHeader() const
Get File Meta Information.
Definition gdcmFile.h:48
gdcm::Global
Global.
Definition gdcmGlobal.h:50
gdcm::Global::GetDicts
Dicts const & GetDicts() const
gdcm::Global::GetInstance
static Global & GetInstance()
return the singleton instance
gdcm::Item
Class to represent an Item.
Definition gdcmItem.h:46
gdcm::Item::GetNestedDataSet
const DataSet & GetNestedDataSet() const
Definition gdcmItem.h:80
gdcm::MediaStorage
MediaStorage.
Definition gdcmMediaStorage.h:44
gdcm::MediaStorage::RawDataStorage
@ RawDataStorage
Definition gdcmMediaStorage.h:85
gdcm::SequenceOfItems
Class to represent a Sequence Of Items.
Definition gdcmSequenceOfItems.h:40
gdcm::SmartPointer
Class for Smart Pointer.
Definition gdcmSmartPointer.h:40
gdcm::Tag
Class to represent a DICOM Data Element (Attribute) Tag (Group, Element).
Definition gdcmTag.h:39
gdcm::TransferSyntax::ExplicitVRLittleEndian
@ ExplicitVRLittleEndian
Definition gdcmTransferSyntax.h:64
gdcm::UIDGenerator
Class for generating unique UID.
Definition gdcmUIDGenerator.h:28
gdcm::UIDGenerator::Generate
const char * Generate()
gdcm::VR
VR class.
Definition gdcmVR.h:55
gdcm::VR::SQ
@ SQ
Definition gdcmVR.h:81
gdcm::Writer
Writer ala DOM (Document Object Model).
Definition gdcmWriter.h:49
gdcm::Writer::GetFile
File & GetFile()
Definition gdcmWriter.h:67
gdcm::Writer::SetCheckFileMetaInformation
void SetCheckFileMetaInformation(bool b)
Undocumented function, do not use (= leave default).
Definition gdcmWriter.h:70
gdcm::Writer::Write
virtual bool Write()
Main function to tell the writer to write.
gdcm::Writer::SetFileName
void SetFileName(const char *filename_native)
Set the filename of DICOM file to write:
gdcmAnonymizer.h
gdcmAttribute.h
gdcmDictEntry.h
gdcmDict.h
gdcmDicts.h
gdcmDummyValueGenerator.h
gdcm_assert
#define gdcm_assert(cond)
Definition gdcmException.h:104
gdcmFile.h
gdcmGlobal.h
gdcmImageReader.h
gdcmItem.h
gdcmMediaStorage.h
gdcmReader.h
gdcmSequenceOfItems.h
gdcmTag.h
gdcmTransferSyntax.h
gdcmUIDGenerator.h
gdcmWriter.h