#include "itkImageFileReader.h"
#include "itkGDCMImageIO.h"
#include "itkMetaDataObject.h"
#include "gdcmGlobal.h"
int main( int argc, char* argv[] )
{
if( argc < 2 )
{
std::cerr << "Usage: " << argv[0] << " DicomFile [user defined dict]" << std::endl;
return EXIT_FAILURE;
}
typedef signed short PixelType;
const unsigned int Dimension = 2;
typedef itk::Image< PixelType, Dimension > ImageType;
if( argc == 3 )
{
// Specify a path where XML dicts can be found (Part 3/4 & 6)
gdcm::Global::GetInstance().Prepend( itksys::SystemTools::GetFilenamePath(argv[2]).c_str() );
// Load them !
gdcm::Global::GetInstance().LoadResourcesFiles();
}
typedef itk::ImageFileReader< ImageType > ReaderType;
ReaderType::Pointer reader = ReaderType::New();
typedef itk::GDCMImageIO ImageIOType;
ImageIOType::Pointer dicomIO = ImageIOType::New();
reader->SetFileName( argv[1] );
reader->SetImageIO( dicomIO );
try
{
// Software Guide : BeginCodeSnippet
reader->Update();
// Software Guide : EndCodeSnippet
}
catch (itk::ExceptionObject &ex)
{
std::cout << ex << std::endl;
return EXIT_FAILURE;
}
typedef itk::MetaDataDictionary DictionaryType;
const DictionaryType & dictionary = dicomIO->GetMetaDataDictionary();
typedef itk::MetaDataObject< std::string > MetaDataStringType;
DictionaryType::ConstIterator itr = dictionary.Begin();
DictionaryType::ConstIterator end = dictionary.End();
while( itr != end )
{
itk::MetaDataObjectBase::Pointer entry = itr->second;
MetaDataStringType::Pointer entryvalue =
dynamic_cast<MetaDataStringType *>( entry.GetPointer() );
if( entryvalue )
{
std::string tagkey = itr->first;
std::string labelId;
bool found = itk::GDCMImageIO::GetLabelFromTag( tagkey, labelId );
std::string tagvalue = entryvalue->GetMetaDataObjectValue();
if( found )
{
std::cout << "(" << tagkey << ") " << labelId;
std::cout << " = " << tagvalue.c_str() << std::endl;
}
else
{
std::cout << "(" << tagkey << ") " << "Unknown";
std::cout << " = " << tagvalue.c_str() << std::endl;
}
}
++itr;
} //while
std::string entryId = "0010|0010";
DictionaryType::ConstIterator tagItr = dictionary.Find( entryId );
if( tagItr != end )
{
MetaDataStringType::ConstPointer entryvalue =
dynamic_cast<const MetaDataStringType *>(
tagItr->second.GetPointer() );
if( entryvalue )
{
std::string tagvalue = entryvalue->GetMetaDataObjectValue();
std::cout << "Patient's Name (" << entryId << ") ";
std::cout << " is: " << tagvalue.c_str() << std::endl;
}
// Software Guide : EndCodeSnippet
}
std::string tagkey = "0008|1050";
std::string labelId;
if( itk::GDCMImageIO::GetLabelFromTag( tagkey, labelId ) )
{
std::string value;
std::cout << labelId << " (" << tagkey << "): ";
if( dicomIO->GetValueFromTag(tagkey, value) )
{
std::cout << value;
}
else
{
std::cout << "(No Value Found in File)";
}
std::cout << std::endl;
}
else
{
std::cerr << "Trying to access inexistant DICOM tag." << std::endl;
}
// Software Guide : EndCodeSnippet
itk::ImageIOBase::IOPixelType pixelType
= reader->GetImageIO()->GetPixelType();
itk::ImageIOBase::IOComponentType componentType
= reader->GetImageIO()->GetComponentType();
std::cout << "PixelType: " << reader->GetImageIO()
->GetPixelTypeAsString(pixelType) << std::endl;
std::cout << "Component Type: " << reader->GetImageIO()
->GetComponentTypeAsString(componentType) << std::endl;
// Software Guide : EndCodeSnippet
return EXIT_SUCCESS;
}