GDCM: ReadAndPrintAttributes.cxx

GDCM 3.2.8

ReadAndPrintAttributes.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.
=========================================================================*/
/*
* This small example will show how one can read and print
* a DICOM Attribute using different technique (by tag or by name)
*/
#include "gdcmReader.h"
#include "gdcmGlobal.h"
#include "gdcmDicts.h"
#include "gdcmDict.h"
#include "gdcmAttribute.h"
#include "gdcmStringFilter.h"
#include <iostream>
int main(int argc, char *argv[])
{
if( argc < 2 )
{
std::cerr << argv[0] << " input.dcm" << std::endl;
return 1;
}
const char *filename = argv[1];
// Instantiate the reader:
gdcm::Reader reader;
reader. SetFileName( filename );
if( !reader. Read() )
{
std::cerr << "Could not read: " << filename << std::endl;
return 1;
}
// The output of gdcm::Reader is a gdcm::File
gdcm::File &file = reader. GetFile();
// the dataset is the the set of element we are interested in:
gdcm::DataSet &ds = file. GetDataSet();
const gdcm::Global& g = gdcm::Global::GetInstance();
const gdcm::Dicts &dicts = g. GetDicts();
const gdcm::Dict &pubdict = dicts. GetPublicDict();
using namespace gdcm;
// In this example we will show why using name to lookup attribute can be
// dangerous.
Tag tPatientName(0x0,0x0);
//const DictEntry &de1 =
pubdict. GetDictEntryByName("Patient Name", tPatientName);
std::cout << "Found: " << tPatientName << std::endl;
// Indeed the attribute could not be found. Since DICOM 2003, Patient Name
// has become Patient's Name.
Tag tPatientsName;
//const DictEntry &de2 =
pubdict.GetDictEntryByName("Patient's Name", tPatientsName);
std::cout << "Found: " << tPatientsName << std::endl;
// Let's try to read an arbitrary DICOM Attribute:
Tag tDoseGridScaling;
//const DictEntry &de3 =
pubdict.GetDictEntryByName("Dose Grid Scaling", tDoseGridScaling);
std::cout << "Found: " << tDoseGridScaling << std::endl;
if( ds. FindDataElement( tDoseGridScaling ) )
{
gdcm::StringFilter sf;
sf. SetFile(file);
std::cout << "Attribute Value as String: " << sf. ToString( tDoseGridScaling ) << std::endl;
// Let's check the name again:
std::pair<std::string, std::string> pss
= sf. ToStringPair( tDoseGridScaling );
std::cout << "Attribute Name Checked: " << pss.first << std::endl;
std::cout << "Attribute Value (string): " << pss.second << std::endl;
//const DataElement &dgs = ds.GetDataElement( tDoseGridScaling );
// Let's assume for a moment we knew the tag number:
Attribute<0x3004,0x000e> at;
gdcm_assert( at. GetTag() == tDoseGridScaling );
at. SetFromDataSet( ds );
// For the sake of long term maintenance, we will not write
// that this particular attribute is stored as a double. What if
// a user made a mistake. It is much safer to rely on GDCM internal
// mechanism to deduce the VR::DS type (represented as a ieee double)
Attribute<0x3004,0x000e>::ArrayType v = at. GetValue();
std::cout << "DoseGridScaling=" << v << std::endl;
}
return 0;
}
gdcm::Attribute
Attribute class This class use template metaprograming tricks to let the user know when the template ...
Definition gdcmAttribute.h:86
gdcm::Attribute::GetValue
ArrayType & GetValue(unsigned int idx=0)
Definition gdcmAttribute.h:154
gdcm::Attribute::GetTag
static Tag GetTag()
Definition gdcmAttribute.h:98
gdcm::Attribute::ArrayType
VRToType< TVR >::Type ArrayType
Definition gdcmAttribute.h:88
gdcm::Attribute::SetFromDataSet
void SetFromDataSet(DataSet const &ds)
Definition gdcmAttribute.h:229
gdcm::DataSet
Class to represent a Data Set (which contains Data Elements).
Definition gdcmDataSet.h:56
gdcm::DataSet::FindDataElement
bool FindDataElement(const PrivateTag &t) const
Look up if private tag 't' is present in the dataset:
gdcm::Dict
Class to represent a map of DictEntry.
Definition gdcmDict.h:45
gdcm::Dict::GetDictEntryByName
const DictEntry & GetDictEntryByName(const char *name, Tag &tag) const
Definition gdcmDict.h:157
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::File
a DICOM File
Definition gdcmFile.h:34
gdcm::File::GetDataSet
const DataSet & GetDataSet() const
Get Data Set.
Definition gdcmFile.h:57
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::Reader
Reader ala DOM (Document Object Model).
Definition gdcmReader.h:54
gdcm::Reader::GetFile
const File & GetFile() const
Set/Get File.
Definition gdcmReader.h:72
gdcm::Reader::Read
virtual bool Read()
Main function to read a file.
gdcm::Reader::SetFileName
void SetFileName(const char *filename_native)
gdcm::StringFilter
StringFilter.
Definition gdcmStringFilter.h:30
gdcm::StringFilter::ToString
std::string ToString(const DataElement &de) const
gdcm::StringFilter::ToStringPair
std::pair< std::string, std::string > ToStringPair(const DataElement &de) const
gdcm::StringFilter::SetFile
void SetFile(const File &f)
Set/Get File.
Definition gdcmStringFilter.h:63
gdcm::Tag
Class to represent a DICOM Data Element (Attribute) Tag (Group, Element).
Definition gdcmTag.h:39
gdcmAttribute.h
gdcmDict.h
gdcmDicts.h
gdcm_assert
#define gdcm_assert(cond)
Definition gdcmException.h:104
gdcmGlobal.h
gdcmReader.h
gdcmStringFilter.h
gdcm
Definition gdcmASN1.h:21