1 #include <creaImageIODicomImageReader2.h>
5 #include <creaImageIOSystem.h>
6 #include "boost/filesystem/path.hpp"
8 #include <creaImageIOTreeAttributeDescriptor.h>
9 #include <vtkStringArray.h>
10 #include <creaImageIOGimmick.h>
17 //=====================================================================
18 DicomImageReader::DicomImageReader()
20 mReader = vtkGDCMImageReader::New();
24 //=====================================================================
26 //=====================================================================
27 DicomImageReader::~DicomImageReader()
31 //=====================================================================
33 //=====================================================================
34 bool DicomImageReader::CanRead(const std::string& filename)
37 reader.SetFileName( filename.c_str() );
41 //=====================================================================
43 //=====================================================================
44 vtkImageData* DicomImageReader::ReadImage(const std::string& filename)
49 mReader->SetFileName(filename.c_str());
51 im = vtkImageData::New();
52 im->ShallowCopy(mReader->GetOutput());
56 if (im!=0) im->Delete();
62 //=====================================================================
63 void DicomImageReader::PushBackExtensions(std::vector<std::string>& v)
68 //=====================================================================
70 //========================================================================
71 std::string irclean(const std::string& str)
75 if (str == "GDCM::Unfound")
79 if (str[str.size()-1]==' ')
81 return irclean(str.substr(0,str.size()-1));
83 if (str[str.size()-1]==0)
85 return irclean(str.substr(0,str.size()-1));
91 //========================================================================
93 //=====================================================================
94 void DicomImageReader::ReadAttributes(const std::string& filename,
95 std::map<std::string,std::string>& attr)
97 GimmickMessage(2,"Reading attributes from DICOM file '"
98 <<filename<<"'"<<std::endl);
102 reader.SetFileName( filename.c_str() );
105 std::map<std::string,std::string>::iterator i;
106 for (i=attr.begin();i!=attr.end();++i)
108 if ( i->first == "D0004_1500" )
110 boost::filesystem::path full_path(filename);
111 std::string f = full_path.leaf();
114 else if ( i->first == "FullFileName" )
116 i->second = filename;
118 else if ( i->first == "FullFileDirectory" )
120 std::string::size_type last_pos = filename.find_last_of("//");
121 //find first separator
122 i->second = filename.substr(0, last_pos);
129 tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
130 if ( ( gr!=0 ) && ( el!=0 ) )
132 gdcm::DataElement de( gdcm::Tag(gr,el) );
133 std::string val = GetStringValueFromTag(reader.GetFile().GetDataSet().GetDataElement(gdcm::Tag(gr,el)));
134 i->second = irclean(val);
141 const std::string DicomImageReader::GetStringValueFromTag(const gdcm::DataElement& de)
143 static std::string buffer;
144 buffer = ""; // cleanup previous call
147 const gdcm::ByteValue *bv = de.GetByteValue();
148 if( bv ) // Can be Type 2
150 buffer = std::string( bv->GetPointer(), bv->GetLength() );
151 // Will be padded with at least one \0
155 // Since return is a const char* the very first \0 will be considered
156 return buffer.c_str();
158 //=====================================================================
160 } // namespace creaImageIO