2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
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
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.
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
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 # ------------------------------------------------------------------------
29 #include <creaImageIODicomImageReader.h>
30 #include <vtkGdcmReader.h>
31 #include <creaImageIOSystem.h>
32 #include "boost/filesystem/path.hpp"
34 #include <creaImageIOTreeAttributeDescriptor.h>
35 #include <vtkStringArray.h>
36 #include <creaImageIOGimmick.h>
43 //=====================================================================
44 DicomImageReader::DicomImageReader()
46 mReader = vtkGdcmReader::New();
48 //EED 21 mars 2012 FLIP probleme ..PLOP..
49 mReader->SetFlipY(false);
54 //=====================================================================
56 //=====================================================================
57 DicomImageReader::~DicomImageReader()
61 //=====================================================================
63 //=====================================================================
64 bool DicomImageReader::CanRead(const std::string& filename)
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 );
71 bool ok = file->IsReadable();
74 doc = (GDCM_NAME_SPACE::Document*)file;
75 ok = doc->IsReadable();
80 //=====================================================================
82 //=====================================================================
83 vtkImageData* DicomImageReader::ReadImage(const std::string& filename)
88 mReader->SetFileName(filename.c_str());
90 im = vtkImageData::New();
91 im->ShallowCopy(mReader->GetOutput());
93 if (im!=0) im->Delete();
100 //=====================================================================
101 void DicomImageReader::PushBackExtensions(std::vector<std::string>& v)
106 //=====================================================================
108 //========================================================================
109 std::string irclean(const std::string& str)
113 if (str == "GDCM::Unfound")
117 if (str[str.size()-1]==' ')
119 return irclean(str.substr(0,str.size()-1));
121 if (str[str.size()-1]==0)
123 return irclean(str.substr(0,str.size()-1));
129 //========================================================================
130 void DicomImageReader::getAttributes(const std::string filename,
131 std::map <std::string , std::string> &infos, std::vector<std::string> i_attr)
133 std::vector<std::string>::iterator it = i_attr.begin();
134 for(; it != i_attr.end(); it++)
138 ReadAttributes(filename, infos);
140 //=====================================================================
141 void DicomImageReader::ReadAttributes(const std::string& filename,
142 std::map<std::string,std::string>& attr)
144 GimmickMessage(2,"Reading attributes from DICOM file '"
145 <<filename<<"'"<<std::endl);
147 GDCM_NAME_SPACE::File* file = GDCM_NAME_SPACE::File::New();
148 //boost::shared_ptr<GDCM_NAME_SPACE::File> file(GDCM_NAME_SPACE::File::New());//, DicomImageReader::deleter());
150 GDCM_NAME_SPACE::Document *doc= GDCM_NAME_SPACE::File::New();
151 doc->SetLoadMode( GDCM_NAME_SPACE::LD_ALL);
152 doc->SetFileName(filename.c_str());
154 file->SetLoadMode( GDCM_NAME_SPACE::LD_ALL);
155 file->SetFileName(filename.c_str());
157 if (file->IsReadable())// ||((GDCM_NAME_SPACE::Document*) file)->IsReadable())
159 std::map<std::string,std::string>::iterator i;
160 for (i=attr.begin();i!=attr.end();++i)
162 if ( i->first == "D0004_1500" )
164 boost::filesystem::path full_path(filename);
165 std::string f = full_path.leaf().string();
168 else if ( i->first == "FullFileName" )
170 i->second = filename;
172 else if ( i->first == "FullFileDirectory" )
174 std::string::size_type last_pos = filename.find_last_of("//");
175 //find first separator
176 i->second = filename.substr(0, last_pos);
183 tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
184 if ( ( gr!=0 ) && ( el!=0 ) )
186 std::string val = file->GetEntryString(gr,el);
187 i->second = irclean(val);
194 //=====================================================================
196 } // namespace creaImageIO