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>
19 //=====================================================================
20 DicomImageReader::DicomImageReader()
22 mReader = vtkGDCMImageReader::New();
26 //=====================================================================
28 //=====================================================================
29 DicomImageReader::~DicomImageReader()
33 //=====================================================================
35 //=====================================================================
36 bool DicomImageReader::CanRead(const std::string& filename)
39 reader.SetFileName( filename.c_str() );
43 //=====================================================================
45 //=====================================================================
46 vtkImageData* DicomImageReader::ReadImage(const std::string& filename)
51 mReader->SetFileName(filename.c_str());
53 im = vtkImageData::New();
54 im->ShallowCopy(mReader->GetOutput());
58 if (im!=0) im->Delete();
64 //=====================================================================
65 void DicomImageReader::PushBackExtensions(std::vector<std::string>& v)
70 //=====================================================================
72 //========================================================================
73 std::string irclean(const std::string& str)
77 if (str == "GDCM::Unfound")
81 if (str[str.size()-1]==' ')
83 return irclean(str.substr(0,str.size()-1));
85 if (str[str.size()-1]==0)
87 return irclean(str.substr(0,str.size()-1));
93 //========================================================================
95 //=====================================================================
96 void DicomImageReader::ReadAttributes(const std::string& filename,
97 std::map<std::string,std::string>& attr)
99 GimmickMessage(2,"Reading attributes from DICOM file '"
100 <<filename<<"'"<<std::endl);
104 reader.SetFileName( filename.c_str() );
107 std::map<std::string,std::string>::iterator i;
108 for (i=attr.begin();i!=attr.end();++i)
110 if ( i->first == "D0004_1500" )
112 boost::filesystem::path full_path(filename);
113 std::string f = full_path.leaf();
116 else if ( i->first == "FullFileName" )
118 i->second = filename;
120 else if ( i->first == "FullFileDirectory" )
122 std::string::size_type last_pos = filename.find_last_of("//");
123 //find first separator
124 i->second = filename.substr(0, last_pos);
131 tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
132 if ( ( gr!=0 ) && ( el!=0 ) )
134 gdcm::DataElement de( gdcm::Tag(gr,el) );
135 std::string val = GetStringValueFromTag(reader.GetFile().GetDataSet().GetDataElement(gdcm::Tag(gr,el)));
136 i->second = irclean(val);
143 void DicomImageReader::ReadAttributes2(const std::string& filename,
144 std::map<std::string,std::string>& attr)
149 std::map<std::string,std::string>::iterator i;
150 for (i=attr.begin();i!=attr.end();++i)
152 if ( i->first == "D0004_1500" || i->first == "FullFileName" || i->first == "FullFileDirectory" )
161 tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
162 mscan.AddTag(gdcm::Tag(gr,el) );
167 bool b = mscan.IsKey(filename.c_str());
170 const gdcm::Scanner::TagToValue &mapping = mscan.GetMapping(filename.c_str());
171 gdcm::Scanner::TagToValue::const_iterator it = mapping.begin();
172 std::map<std::string, std::string>::iterator i;
173 for (i=attr.begin();i!=attr.end();++i, ++it)
175 if ( i->first == "D0004_1500" )
177 boost::filesystem::path full_path(filename);
178 std::string f = full_path.leaf();
181 else if ( i->first == "FullFileName" )
183 i->second = filename;
185 else if ( i->first == "FullFileDirectory" )
187 std::string::size_type last_pos = filename.find_last_of("//");
188 //find first separator
189 i->second = filename.substr(0, last_pos);
193 const char *value = it->second;
194 i->second = irclean(it->second);
215 const std::string DicomImageReader::GetStringValueFromTag(const gdcm::DataElement& de)
217 static std::string buffer;
218 buffer = ""; // cleanup previous call
221 const gdcm::ByteValue *bv = de.GetByteValue();
222 if( bv ) // Can be Type 2
224 buffer = std::string( bv->GetPointer(), bv->GetLength() );
225 // Will be padded with at least one \0
229 // Since return is a const char* the very first \0 will be considered
230 return buffer.c_str();
232 //=====================================================================
234 } // namespace creaImageIO