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