]> Creatis software - creaImageIO.git/blob - src/creaImageIODicomImageReader2.cpp
7361034b5e50c032797e7b7dbf4a9bf880eb5249
[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                          
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         if(!b_loaded)
147         {
148                 std::map<std::string,std::string>::iterator i;
149                 for (i=attr.begin();i!=attr.end();++i)
150                   {
151                         if ( i->first == "D0004_1500" ||  i->first == "FullFileName" || i->first == "FullFileDirectory" )
152                         {
153                         
154                         }
155                         else
156                         {
157                         uint16_t el;
158                         uint16_t gr;
159                         
160                         tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
161                         mscan.AddTag(gdcm::Tag(gr,el) );
162                         }
163                 }
164                 b_loaded = true;
165         }
166          bool b = mscan.IsKey(filename.c_str());
167      if( b )
168       {
169                   const gdcm::Scanner::TagToValue &mapping = mscan.GetMapping(filename.c_str());
170                   gdcm::Scanner::TagToValue::const_iterator it = mapping.begin();
171                   std::map<std::string, std::string>::iterator i;
172                 for (i=attr.begin();i!=attr.end();++i, ++it)
173                 {
174                         if ( i->first == "D0004_1500" )
175                         {
176                                 boost::filesystem::path full_path(filename);
177                                 std::string f = full_path.leaf();
178                                 i->second = f;
179                         }
180                         else if ( i->first == "FullFileName" )
181                         {
182                                 i->second = filename;
183                         }
184                         else if ( i->first == "FullFileDirectory" )
185                         {
186                                   std::string::size_type last_pos = filename.find_last_of("//");
187                                   //find first separator
188                                   i->second = filename.substr(0, last_pos);
189                         }
190                         else
191                         {
192                                 const char *value = it->second;
193                                 i->second = irclean(it->second);
194                         }
195                 }
196          }
197 }
198
199 const std::string DicomImageReader::GetStringValueFromTag(const gdcm::DataElement& de)
200 {
201   static std::string buffer;
202   buffer = "";  // cleanup previous call
203
204
205     const gdcm::ByteValue *bv = de.GetByteValue();
206     if( bv ) // Can be Type 2
207       {
208       buffer = std::string( bv->GetPointer(), bv->GetLength() );
209       // Will be padded with at least one \0
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