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;
140 for (;it != mapping.end(); ++it)
142 /*std::stringstream val;
144 val << std::hex << it->first.GetGroup() ;
146 val << std::hex << it->first.GetElement();
150 sprintf(key,"D%04x_%04x", it->first.GetGroup(), it->first.GetElement());
151 attr[key] = irclean(it->second);
158 for (i=attr.begin();i!=attr.end();++i, j++)
160 if ( attr.find("D0004_1500") != attr.end())
162 boost::filesystem::path full_path(filename);
163 std::string f = full_path.leaf();
164 attr["D0004_1500"] = f;
166 if ( attr.find("FullFileName")!= attr.end())
168 attr["FullFileName"] = filename;
170 if ( attr.find("FullFileDirectory" )!= attr.end())
172 std::string::size_type last_pos = filename.find_last_of("//");
173 //find first separator
174 attr["FullFileDirectory" ] = filename.substr(0, last_pos);
182 // tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
184 // mscan.AddTag(gdcm::Tag(gr,el) );
186 // // const char *value = it->second;
187 // i->second = irclean(it->second);
209 const std::string DicomImageScanner::GetStringValueFromTag(const gdcm::DataElement& de)
211 static std::string buffer;
212 buffer = ""; // cleanup previous call
215 const gdcm::ByteValue *bv = de.GetByteValue();
216 if( bv ) // Can be Type 2
218 buffer = std::string( bv->GetPointer(), bv->GetLength() );
219 // Will be padded with at least one \0
223 // Since return is a const char* the very first \0 will be considered
224 return buffer.c_str();
226 //=====================================================================
228 } // namespace creaImageIO