]> Creatis software - creaImageIO.git/blob - src/creaImageIODicomImageReader.cpp
bfc40893df5f4ca9aabbe46fb31d504661e49e60
[creaImageIO.git] / src / creaImageIODicomImageReader.cpp
1 #include <creaImageIODicomImageReader.h>
2
3 #include <vtkGdcmReader.h>
4
5
6
7 #include <creaImageIOSystem.h>
8 #include "boost/filesystem/path.hpp"
9
10 #include <creaImageIOTreeAttributeDescriptor.h>
11 #include <vtkStringArray.h>
12 #include <creaImageIOGimmick.h>
13 #ifdef _DEBUG
14 #define new DEBUG_NEW
15 #endif
16 namespace creaImageIO
17 {
18
19   //=====================================================================
20   DicomImageReader::DicomImageReader()
21   {
22     mReader = vtkGdcmReader::New();
23           
24 //EED 21 mars 2012  FLIP probleme  ..PLOP..
25         mReader->SetFlipY(false);
26           
27     SetName ( "Dicom" );
28
29   };
30   //=====================================================================
31   
32   //=====================================================================
33   DicomImageReader::~DicomImageReader()
34   {
35     mReader->Delete();
36   }
37   //=====================================================================
38
39   //=====================================================================  
40   bool DicomImageReader::CanRead(const std::string& filename)
41   { 
42     GDCM_NAME_SPACE::Document*doc;
43     GDCM_NAME_SPACE::File* file = GDCM_NAME_SPACE::File::New();
44     file->SetLoadMode( GDCM_NAME_SPACE::LD_ALL);
45     file->SetFileName(filename.c_str());
46     file->Load();
47     bool ok = file->IsReadable();
48         if(!ok)
49         {
50                 doc = (GDCM_NAME_SPACE::Document*)file; 
51                 ok = doc->IsReadable();
52         }
53     file->Delete();
54     return ok;
55   }
56   //=====================================================================
57
58   //=====================================================================
59   vtkImageData* DicomImageReader::ReadImage(const std::string& filename)
60   {
61     vtkImageData* im = 0;
62     try
63       {
64         mReader->SetFileName(filename.c_str());
65         mReader->Update();
66         im = vtkImageData::New();
67         im->ShallowCopy(mReader->GetOutput());
68       }
69     catch (...)
70       {
71         if (im!=0) im->Delete();
72           im = 0;
73       }
74     return im;
75   }
76
77   //=====================================================================
78   void DicomImageReader::PushBackExtensions(std::vector<std::string>& v)
79   {
80     v.push_back("dcm");
81     v.push_back("");
82   }
83   //=====================================================================
84   
85   //========================================================================
86   std::string irclean(const std::string& str)
87   {
88           if(str.size() > 0)
89           {
90                 if (str == "GDCM::Unfound") 
91                   {
92                 return "";
93                   }
94                 if (str[str.size()-1]==' ')
95                   {
96                 return irclean(str.substr(0,str.size()-1));
97                   }
98                 if (str[str.size()-1]==0)
99                   {
100                 return irclean(str.substr(0,str.size()-1));
101                   }
102           }
103     
104     return str;
105   }
106   //========================================================================
107   void DicomImageReader::getAttributes(const std::string filename,
108                 std::map <std::string , std::string> &infos, std::vector<std::string> i_attr)
109         {
110                 std::vector<std::string>::iterator it = i_attr.begin();
111                 for(; it != i_attr.end(); it++)
112                 {
113                         infos[(*it)] = "";
114                 }
115                 ReadAttributes(filename, infos);
116         }
117   //=====================================================================
118   void DicomImageReader::ReadAttributes(const std::string& filename, 
119                       std::map<std::string,std::string>& attr)
120   {
121     GimmickMessage(2,"Reading attributes from DICOM file '"
122                    <<filename<<"'"<<std::endl);
123
124     GDCM_NAME_SPACE::File* file = GDCM_NAME_SPACE::File::New();
125         //boost::shared_ptr<GDCM_NAME_SPACE::File> file(GDCM_NAME_SPACE::File::New());//, DicomImageReader::deleter());
126
127     GDCM_NAME_SPACE::Document *doc= GDCM_NAME_SPACE::File::New();
128     doc->SetLoadMode( GDCM_NAME_SPACE::LD_ALL);
129     doc->SetFileName(filename.c_str());
130     doc->Load();
131     file->SetLoadMode( GDCM_NAME_SPACE::LD_ALL);
132     file->SetFileName(filename.c_str());
133     file->Load();
134     if (file->IsReadable())// ||((GDCM_NAME_SPACE::Document*) file)->IsReadable())
135       {
136         std::map<std::string,std::string>::iterator i;
137         for (i=attr.begin();i!=attr.end();++i)
138           {
139             if ( i->first == "D0004_1500" )
140               {
141                 boost::filesystem::path full_path(filename);
142                 std::string f = full_path.leaf();
143                 i->second = f;
144               }
145             else if ( i->first == "FullFileName" )
146               {
147                 i->second = filename;
148               }
149                   else if ( i->first == "FullFileDirectory" )
150               {
151                           std::string::size_type last_pos = filename.find_last_of("//");
152                           //find first separator
153                           i->second = filename.substr(0, last_pos);
154               }
155             else
156               {
157                 uint16_t el;
158                 uint16_t gr;
159                 
160                 tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
161                 if ( ( gr!=0 ) && ( el!=0 ) )
162                   {
163                     std::string val =  file->GetEntryString(gr,el);
164                     i->second = irclean(val);
165                   }
166               }
167           }
168       }
169   }
170
171   //=====================================================================
172   
173 } // namespace creaImageIO
174