1 #include <creaImageIODicomScanner.h>
2 #include "creaImageIOSystem.h"
3 #include <boost/filesystem.hpp>
4 #include "boost/algorithm/string.hpp"
8 #include <creaImageIOSystem.h>
9 #include "boost/filesystem/path.hpp"
11 #include <creaImageIOTreeAttributeDescriptor.h>
12 #include <vtkStringArray.h>
13 #include <creaImageIOGimmick.h>
14 #include <gdcmDirectory.h>
25 //=====================================================================
26 DicomImageScanner::DicomImageScanner()
28 mReader = vtkGDCMImageReader::New();
32 //=====================================================================
34 //=====================================================================
35 DicomImageScanner::~DicomImageScanner()
39 //=====================================================================
41 //=====================================================================
42 bool DicomImageScanner::addDirectory(std::string& filename, std::map<std::string,std::string>& attr)
47 std::map<std::string,std::string>::iterator i;
49 for (i=attr.begin();i!=attr.end();++i, j++)
51 if ( i->first == "D0004_1500" || i->first == "FullFileName" || i->first == "FullFileDirectory" )
60 tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
61 mscan.AddTag(gdcm::Tag(gr,el) );
69 boost::algorithm::replace_all(filename,"\\", "/");
70 d.Load(filename.c_str(),true);
71 mscan.Scan(d.GetFilenames());
76 //=====================================================================
78 //=====================================================================
79 vtkImageData* DicomImageScanner::ReadImage(const std::string& filename)
84 mReader->SetFileName(filename.c_str());
86 im = vtkImageData::New();
87 im->ShallowCopy(mReader->GetOutput());
91 if (im!=0) im->Delete();
98 //=====================================================================
100 //========================================================================
101 std::string DicomImageScanner::irclean(const std::string& str)
105 if (str == "GDCM::Unfound")
109 if (str[str.size()-1]==' ')
111 return irclean(str.substr(0,str.size()-1));
113 if (str[str.size()-1]==0)
115 return irclean(str.substr(0,str.size()-1));
121 //========================================================================
123 //=====================================================================
125 void DicomImageScanner::ReadAttributes(const std::string& filename,
126 std::map<std::string,std::string>& attr)
128 std::string name = "E:\\data-images\\dicoms\\CD1\\DICOM\\09112417\\37390000/31582235";
130 bool b = mscan.IsKey(filename.c_str());
132 const gdcm::Scanner::TagToValue &mapping = mscan.GetMapping(filename.c_str());
133 gdcm::Scanner::TagToValue::const_iterator it = mapping.begin();
135 std::map<std::string, std::string>::iterator i;
137 for (;it != mapping.end(); ++it)
139 sprintf(key,"D%04x_%04x", it->first.GetGroup(), it->first.GetElement());
140 attr[key] = irclean(it->second);
147 for (i=attr.begin();i!=attr.end();++i, j++)
149 if ( attr.find("D0004_1500") != attr.end())
151 boost::filesystem::path full_path(filename);
152 std::string f = full_path.leaf();
153 attr["D0004_1500"] = f;
155 if ( attr.find("FullFileName")!= attr.end())
157 attr["FullFileName"] = filename;
159 if ( attr.find("FullFileDirectory" )!= attr.end())
161 std::string::size_type last_pos = filename.find_last_of("//");
162 //find first separator
163 attr["FullFileDirectory" ] = filename.substr(0, last_pos);
171 // tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
173 // mscan.AddTag(gdcm::Tag(gr,el) );
175 // // const char *value = it->second;
176 // i->second = irclean(it->second);
198 const std::string DicomImageScanner::GetStringValueFromTag(const gdcm::DataElement& de)
200 static std::string buffer;
201 buffer = ""; // cleanup previous call
204 const gdcm::ByteValue *bv = de.GetByteValue();
205 if( bv ) // Can be Type 2
207 buffer = std::string( bv->GetPointer(), bv->GetLength() );
208 // Will be padded with at least one \0
212 // Since return is a const char* the very first \0 will be considered
213 return buffer.c_str();
215 //=====================================================================
217 } // namespace creaImageIO