]> Creatis software - creaImageIO.git/blob - src/creaImageIODicomScanner.cpp
43706c156436c2edbf83bf1a27b74859ec3dffaa
[creaImageIO.git] / src / creaImageIODicomScanner.cpp
1 #include <creaImageIODicomScanner.h>
2 #include "creaImageIOSystem.h"
3 #include <boost/filesystem.hpp>
4 #include "boost/algorithm/string.hpp"
5
6
7
8 #include <creaImageIOSystem.h>
9 #include "boost/filesystem/path.hpp"
10
11 #include <creaImageIOTreeAttributeDescriptor.h>
12 #include <vtkStringArray.h>
13 #include <creaImageIOGimmick.h>
14 #include <gdcmDirectory.h>
15
16 #include <fstream>
17
18
19 #ifdef _DEBUG
20 #define new DEBUG_NEW
21 #endif
22 namespace creaImageIO
23 {
24
25   //=====================================================================
26   DicomImageScanner::DicomImageScanner()
27   {
28           mReader =  vtkGDCMImageReader::New();
29         mscan.ClearTags();
30         b_loaded = false;
31   };
32   //=====================================================================
33   
34   //=====================================================================
35   DicomImageScanner::~DicomImageScanner()
36   {
37           mReader->Delete();
38   }
39   //=====================================================================
40
41   //=====================================================================  
42   bool DicomImageScanner::addDirectory(std::string& filename,  std::map<std::string,std::string>& attr)
43   { 
44                 if(!b_loaded)
45         {
46                 mscan.ClearTags();
47                 std::map<std::string,std::string>::iterator i;
48                 int j= 0;
49                 for (i=attr.begin();i!=attr.end();++i, j++)
50                   {
51                         if ( i->first == "D0004_1500" ||  i->first == "FullFileName" || i->first == "FullFileDirectory" )
52                         {
53                         
54                         }
55                         else
56                         {
57                         uint16_t el;
58                         uint16_t gr;
59                         
60                         tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
61                         mscan.AddTag(gdcm::Tag(gr,el) );
62                         
63                         }
64                 }
65                 b_loaded = true;
66         }
67           gdcm::Directory d;
68             
69           boost::algorithm::replace_all(filename,"\\", "/");
70           d.Load(filename.c_str(),true);
71           mscan.Scan(d.GetFilenames());
72           
73       return  true;
74          
75   }
76   //=====================================================================
77
78   //=====================================================================
79   vtkImageData* DicomImageScanner::ReadImage(const std::string& filename)
80   {
81     vtkImageData* im = 0;
82     try
83       {
84         mReader->SetFileName(filename.c_str());
85         mReader->Update();
86         im = vtkImageData::New();
87         im->ShallowCopy(mReader->GetOutput());
88       }
89     catch (...)
90       {
91         if (im!=0) im->Delete();
92           im = 0;
93       }
94     return im;
95   }
96
97
98   //=====================================================================
99   
100   //========================================================================
101   std::string DicomImageScanner::irclean(const std::string& str)
102   {
103           if(str.size() > 0)
104           {
105                 if (str == "GDCM::Unfound") 
106                   {
107                 return "";
108                   }
109                 if (str[str.size()-1]==' ')
110                   {
111                 return irclean(str.substr(0,str.size()-1));
112                   }
113                 if (str[str.size()-1]==0)
114                   {
115                 return irclean(str.substr(0,str.size()-1));
116                   }
117           }
118     
119     return str;
120   }
121   //========================================================================
122  
123   //=====================================================================
124
125 void DicomImageScanner::ReadAttributes(const std::string& filename, 
126                       std::map<std::string,std::string>& attr)
127   {
128           std::string name = "E:\\data-images\\dicoms\\CD1\\DICOM\\09112417\\37390000/31582235";
129
130          bool b = mscan.IsKey(filename.c_str());
131     if( b )      {
132                   const gdcm::Scanner::TagToValue &mapping = mscan.GetMapping(filename.c_str());
133                   gdcm::Scanner::TagToValue::const_iterator it = mapping.begin();
134
135                   std::map<std::string, std::string>::iterator i;
136                   int j= 0;
137
138                   
139                   
140                   for (;it != mapping.end(); ++it)
141                   {
142                                         /*std::stringstream val;
143                                         val <<  "D";
144                                         val << std::hex << it->first.GetGroup() ;
145                                         val << "_";;
146                                         val << std::hex <<  it->first.GetElement();
147                                         var >> key*/
148                                  char key[12] ;
149                                   
150                                   sprintf(key,"D%04x_%04x", it->first.GetGroup(), it->first.GetElement());
151                                   attr[key] = irclean(it->second);
152                   }
153                   
154                   
155                  /* 
156                   
157                   
158                   for (i=attr.begin();i!=attr.end();++i, j++)
159                 {*/
160                         if ( attr.find("D0004_1500") != attr.end())
161                         {
162                                 boost::filesystem::path full_path(filename);
163                                 std::string f = full_path.leaf();
164                                 attr["D0004_1500"] = f;
165                         }
166                         if ( attr.find("FullFileName")!= attr.end())
167                         {
168                                 attr["FullFileName"] = filename;
169                         }
170                         if ( attr.find("FullFileDirectory" )!= attr.end())
171                         {
172                                   std::string::size_type last_pos = filename.find_last_of("//");
173                                   //find first separator
174                                   attr["FullFileDirectory" ] = filename.substr(0, last_pos);
175                         }
176                 //      else
177                 //      {
178
179                 //              uint16_t el;
180                 //              uint16_t gr;
181
182                 //              tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
183
184                 //      mscan.AddTag(gdcm::Tag(gr,el) );
185
186                 //      //      const char *value = it->second;
187                 //              i->second = irclean(it->second);
188                 //              ++it;
189                 //      }
190                 //}
191         }
192 }
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209   const std::string DicomImageScanner::GetStringValueFromTag(const gdcm::DataElement& de)
210 {
211   static std::string buffer;
212   buffer = "";  // cleanup previous call
213
214
215     const gdcm::ByteValue *bv = de.GetByteValue();
216     if( bv ) // Can be Type 2
217       {
218       buffer = std::string( bv->GetPointer(), bv->GetLength() );
219       // Will be padded with at least one \0
220       }
221
222
223   // Since return is a const char* the very first \0 will be considered
224   return buffer.c_str();
225 }
226   //=====================================================================
227   
228 } // namespace creaImageIO
229