1 #include <creaImageIODicomImageReader2.h>
2 #include "gdcmStringFilter.h"
3 #include "vtkGDCMImageReader.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();
62 //gdcm::Tag grouplength(0x0000,0x0000);
63 //gdcm::DataElement de(gdcm::Tag(0x0000, 0x0000));
64 //de.SetByteValue("0",gdcm::VR::UL);
67 //gdcm::UIDs::GetUIDString(gdcm::UIDS::uid_1_2_840_10008_1_1),gdcm::VR::UI);
74 //=====================================================================
75 void DicomImageReader::PushBackExtensions(std::vector<std::string>& v)
81 //========================================================================
82 std::string irclean(const std::string& str)
86 if (str == "GDCM::Unfound")
90 if (str[str.size()-1]==' ')
92 return irclean(str.substr(0,str.size()-1));
94 if (str[str.size()-1]==0)
96 return irclean(str.substr(0,str.size()-1));
102 //========================================================================
103 //=====================================================================
106 void DicomImageReader::getAttributes(const std::string filename,
107 std::map <std::string , std::string> &infos, std::vector<std::string> i_attr)
110 reader.SetFileName( filename.c_str() );
113 std::vector<std::string>::iterator it = i_attr.begin();
114 for(;it != i_attr.end(); it++)
118 sscanf((*it).c_str(),"D%04hx_%04hx",&gr,&el);
119 if ( ( gr!=0 ) && ( el!=0 ) )
121 infos[(*it)] = ( GetStringValueFromTag(reader.GetFile().GetDataSet().GetDataElement(gdcm::Tag(gr,el))) );
127 //=====================================================================
128 void DicomImageReader::ReadAttributes(const std::string& filename,
129 std::map<std::string,std::string>& attr)
131 GimmickMessage(2,"Reading attributes from DICOM file '"
132 <<filename<<"'"<<std::endl);
136 reader.SetFileName( filename.c_str() );
139 gdcm::StringFilter sf;
140 sf.SetFile(reader.GetFile());
141 std::map<std::string,std::string>::iterator i;
142 for (i=attr.begin();i!=attr.end();++i)
144 if ( i->first == "D0004_1500" )
146 boost::filesystem::path full_path(filename);
147 std::string f = full_path.leaf();
150 else if ( i->first == "FullFileName" )
152 i->second = filename;
154 else if ( i->first == "FullFileDirectory" )
156 std::string::size_type last_pos = filename.find_last_of("//");
157 //find first separator
158 i->second = filename.substr(0, last_pos);
165 gdcm::Attribute< 0x0008,0x103e> at;
170 tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
171 //if ( ( gr!=0 ) && ( el!=0 ) )
172 const gdcm::Tag tag(gr, el);
173 if( reader.GetFile().GetDataSet().FindDataElement( tag ) )
175 i->second = irclean(sf.ToString(tag));
186 void DicomImageReader::ReadAttributes2(const std::string& filename,
187 std::map<std::string,std::string>& attr)
191 std::map<std::string,std::string>::iterator i;
192 for (i=attr.begin();i!=attr.end();++i)
194 if ( i->first == "D0004_1500" || i->first == "FullFileName" || i->first == "FullFileDirectory" )
203 tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
204 mscan.AddTag(gdcm::Tag(gr,el) );
209 bool b = mscan.IsKey(filename.c_str());
212 const gdcm::Scanner::TagToValue &mapping = mscan.GetMapping(filename.c_str());
213 gdcm::Scanner::TagToValue::const_iterator it = mapping.begin();
214 std::map<std::string, std::string>::iterator i;
215 for (i=attr.begin();i!=attr.end();++i, ++it)
217 if ( i->first == "D0004_1500" )
219 boost::filesystem::path full_path(filename);
220 std::string f = full_path.leaf();
223 else if ( i->first == "FullFileName" )
225 i->second = filename;
227 else if ( i->first == "FullFileDirectory" )
229 std::string::size_type last_pos = filename.find_last_of("//");
230 //find first separator
231 i->second = filename.substr(0, last_pos);
235 const char *value = it->second;
236 i->second = irclean(it->second);
242 const std::string DicomImageReader::GetStringValueFromTag(const gdcm::DataElement& de)
244 static std::string buffer;
245 buffer = ""; // cleanup previous call
248 const gdcm::ByteValue *bv = de.GetByteValue();
249 if( bv ) // Can be Type 2
251 buffer = std::string( bv->GetPointer(), bv->GetLength() );
252 // Will be padded with at least one \0
255 // Since return is a const char* the very first \0 will be considered
256 return buffer.c_str();
258 //=====================================================================
260 } // namespace creaImageIO