]> Creatis software - creaImageIO.git/blob - src/creaImageIODicomScanner.cpp
#3185 creaImageIO Feature New Normal - Clean code
[creaImageIO.git] / src / creaImageIODicomScanner.cpp
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and 
11 #  abiding by the rules of distribution of free software. You can  use, 
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
13 #  license as circulated by CEA, CNRS and INRIA at the following URL 
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability. 
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------
26 */
27
28 #include <creaImageIODicomScanner.h>
29
30 #include <boost/filesystem.hpp>
31 #include "boost/algorithm/string.hpp"
32 #include "boost/filesystem/path.hpp"
33
34 #include <fstream>
35 #if defined(_WIN32)
36 #pragma warning(disable: 4996)
37 #endif
38
39 #ifdef _DEBUG
40 #define new DEBUG_NEW
41 #endif
42 namespace creaImageIO
43 {
44
45   //=====================================================================
46   DicomImageScanner::DicomImageScanner()
47   {
48           mReader =  vtkGDCMImageReader::New();
49         mscan.ClearTags();
50         b_loaded = false;
51   };
52   //=====================================================================
53   
54   //=====================================================================
55   DicomImageScanner::~DicomImageScanner()
56   {
57           mReader->Delete();
58   }
59   //=====================================================================
60
61   //=====================================================================  
62   bool DicomImageScanner::addDirectory(std::string& filename,  std::map<std::string,std::string>& attr)
63   { 
64                 if(!b_loaded)
65         {
66                 mscan.ClearTags();
67                 std::map<std::string,std::string>::iterator i;
68                 int j= 0;
69                 for (i=attr.begin();i!=attr.end();++i, j++)
70                   {
71                         if ( i->first == "D0004_1500" ||  i->first == "FullFileName" || i->first == "FullFileDirectory" )
72                         {
73                         
74                         }
75                         else
76                         {
77                         uint16_t el;
78                         uint16_t gr;
79                         
80                         tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
81                         mscan.AddTag(gdcm::Tag(gr,el) );
82                         
83                         }
84                 }
85                 b_loaded = true;
86         }
87           gdcm::Directory d;
88             
89           boost::algorithm::replace_all(filename,"\\", "/");
90           d.Load(filename.c_str(),true);
91           mscan.Scan(d.GetFilenames());
92           
93       return  true;
94          
95   }
96   //=====================================================================
97
98   //=====================================================================
99   vtkImageData* DicomImageScanner::ReadImage(const std::string& filename)
100   {
101     vtkImageData* im = 0;
102     try
103       {
104         mReader->SetFileName(filename.c_str());
105         mReader->Update();
106         im = vtkImageData::New();
107         im->ShallowCopy(mReader->GetOutput());
108       }
109     catch (...)
110       {
111         if (im!=0) im->Delete();
112           im = 0;
113       }
114     return im;
115   }
116
117
118   //=====================================================================
119   
120   //========================================================================
121   std::string DicomImageScanner::irclean(const std::string& str)
122   {
123           if(str.size() > 0)
124           {
125                 if (str == "GDCM::Unfound") 
126                   {
127                 return "";
128                   }
129                 if (str[str.size()-1]==' ')
130                   {
131                 return irclean(str.substr(0,str.size()-1));
132                   }
133                 if (str[str.size()-1]==0)
134                   {
135                 return irclean(str.substr(0,str.size()-1));
136                   }
137           }
138     
139     return str;
140   }
141   //========================================================================
142  
143   //=====================================================================
144
145 void DicomImageScanner::ReadAttributes(const std::string& filename, 
146                       std::map<std::string,std::string>& attr)
147   {
148           std::string name = "E:\\data-images\\dicoms\\CD1\\DICOM\\09112417\\37390000/31582235";
149
150          bool b = mscan.IsKey(filename.c_str());
151     if( b )      {
152                   const gdcm::Scanner::TagToValue &mapping = mscan.GetMapping(filename.c_str());
153                   gdcm::Scanner::TagToValue::const_iterator it = mapping.begin();
154
155                   std::map<std::string, std::string>::iterator i;
156                   
157                   for (;it != mapping.end(); ++it)
158                   {              char key[12] ;
159                                   sprintf(key,"D%04x_%04x", it->first.GetGroup(), it->first.GetElement());
160                                   attr[key] = irclean(it->second);
161                   }
162                   
163                   
164                  /* 
165                   
166                   
167                   for (i=attr.begin();i!=attr.end();++i, j++)
168                 {*/
169                         if ( attr.find("D0004_1500") != attr.end())
170                         {
171                                 boost::filesystem::path full_path(filename);
172                                 std::string f = full_path.leaf().string();
173                                 attr["D0004_1500"] = f;
174                         }
175                         if ( attr.find("FullFileName")!= attr.end())
176                         {
177                                 attr["FullFileName"] = filename;
178                         }
179                         if ( attr.find("FullFileDirectory" )!= attr.end())
180                         {
181                                   std::string::size_type last_pos = filename.find_last_of("//");
182                                   //find first separator
183                                   attr["FullFileDirectory" ] = filename.substr(0, last_pos);
184                         }
185                 //      else
186                 //      {
187
188                 //              uint16_t el;
189                 //              uint16_t gr;
190
191                 //              tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
192
193                 //      mscan.AddTag(gdcm::Tag(gr,el) );
194
195                 //      //      const char *value = it->second;
196                 //              i->second = irclean(it->second);
197                 //              ++it;
198                 //      }
199                 //}
200         }
201 }
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218   const std::string DicomImageScanner::GetStringValueFromTag(const gdcm::DataElement& de)
219 {
220   static std::string buffer;
221   buffer = "";  // cleanup previous call
222
223
224     const gdcm::ByteValue *bv = de.GetByteValue();
225     if( bv ) // Can be Type 2
226       {
227       buffer = std::string( bv->GetPointer(), bv->GetLength() );
228       // Will be padded with at least one \0
229       }
230
231
232   // Since return is a const char* the very first \0 will be considered
233   return buffer.c_str();
234 }
235   //=====================================================================
236   
237 } // namespace creaImageIO
238