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