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