]> Creatis software - creaImageIO.git/blob - src2/creaImageIODicomImageReader.cpp
8ff8b5b102c3fc64e790710e3b95a383b98a909d
[creaImageIO.git] / src2 / 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  
105   //=====================================================================
106   void DicomImageReader::ReadAttributes(const std::string& filename, 
107                       std::map<std::string,std::string>& attr)
108   {
109     GimmickMessage(2,"Reading attributes from DICOM file '"
110                    <<filename<<"'"<<std::endl);
111
112     //GDCM_NAME_SPACE::File* file = GDCM_NAME_SPACE::File::New();
113         boost::shared_ptr<GDCM_NAME_SPACE::File> file(GDCM_NAME_SPACE::File::New(), DicomImageReader::deleter());
114
115     GDCM_NAME_SPACE::Document *doc= GDCM_NAME_SPACE::File::New();
116     doc->SetLoadMode( GDCM_NAME_SPACE::LD_ALL);
117     doc->SetFileName(filename.c_str());
118     doc->Load();
119     file->SetLoadMode( GDCM_NAME_SPACE::LD_ALL);
120     file->SetFileName(filename.c_str());
121     file->Load();
122     if (file->IsReadable())// ||((GDCM_NAME_SPACE::Document*) file)->IsReadable())
123       {
124         std::map<std::string,std::string>::iterator i;
125         for (i=attr.begin();i!=attr.end();++i)
126           {
127             if ( i->first == "D0004_1500" )
128               {
129                 boost::filesystem::path full_path(filename);
130                 std::string f = full_path.leaf();
131                 i->second = f;
132               }
133             else if ( i->first == "FullFileName" )
134               {
135                 i->second = filename;
136               }
137                   else if ( i->first == "FullFileDirectory" )
138               {
139                           std::string::size_type last_pos = filename.find_last_of("//");
140                           //find first separator
141                           i->second = filename.substr(0, last_pos);
142               }
143             else
144               {
145                 uint16_t el;
146                 uint16_t gr;
147                 
148                 tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
149                 if ( ( gr!=0 ) && ( el!=0 ) )
150                   {
151                     std::string val =  file->GetEntryString(gr,el);
152                     i->second = irclean(val);
153                   }
154               }
155           }
156       }
157   }
158
159   //=====================================================================
160   
161 } // namespace creaImageIO
162