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