]> Creatis software - creaImageIO.git/blob - src/creaImageIODicomScanner.cpp
88fe948b33081c735ce0176717a8137090434461
[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                   
137                   for (;it != mapping.end(); ++it)
138                   {              char key[12] ;
139                                   sprintf(key,"D%04x_%04x", it->first.GetGroup(), it->first.GetElement());
140                                   attr[key] = irclean(it->second);
141                   }
142                   
143                   
144                  /* 
145                   
146                   
147                   for (i=attr.begin();i!=attr.end();++i, j++)
148                 {*/
149                         if ( attr.find("D0004_1500") != attr.end())
150                         {
151                                 boost::filesystem::path full_path(filename);
152                                 std::string f = full_path.leaf();
153                                 attr["D0004_1500"] = f;
154                         }
155                         if ( attr.find("FullFileName")!= attr.end())
156                         {
157                                 attr["FullFileName"] = filename;
158                         }
159                         if ( attr.find("FullFileDirectory" )!= attr.end())
160                         {
161                                   std::string::size_type last_pos = filename.find_last_of("//");
162                                   //find first separator
163                                   attr["FullFileDirectory" ] = filename.substr(0, last_pos);
164                         }
165                 //      else
166                 //      {
167
168                 //              uint16_t el;
169                 //              uint16_t gr;
170
171                 //              tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
172
173                 //      mscan.AddTag(gdcm::Tag(gr,el) );
174
175                 //      //      const char *value = it->second;
176                 //              i->second = irclean(it->second);
177                 //              ++it;
178                 //      }
179                 //}
180         }
181 }
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198   const std::string DicomImageScanner::GetStringValueFromTag(const gdcm::DataElement& de)
199 {
200   static std::string buffer;
201   buffer = "";  // cleanup previous call
202
203
204     const gdcm::ByteValue *bv = de.GetByteValue();
205     if( bv ) // Can be Type 2
206       {
207       buffer = std::string( bv->GetPointer(), bv->GetLength() );
208       // Will be padded with at least one \0
209       }
210
211
212   // Since return is a const char* the very first \0 will be considered
213   return buffer.c_str();
214 }
215   //=====================================================================
216   
217 } // namespace creaImageIO
218