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 <creaImageIOImageReader.h>
30 #include <creaImageIOTreeAttributeDescriptor.h>
31 #include <creaImageIOSystem.h>
33 #include <creaImageIOVtkImageReader.h>
34 #if defined (USE_GDCM)
35 #include <creaImageIODicomImageReader.h>
37 #if defined(USE_GDCM2)
38 #include <creaImageIODicomImageReader2.h>
40 #include <creaImageIOUltrasonixImageReader.h>
41 #include <vtkPNGReader.h>
42 #include <vtkTIFFReader.h>
43 #include <vtkJPEGReader.h>
44 #include <vtkBMPReader.h>
45 #include <vtkSLCReader.h>
46 #include <vtkMetaImageReader.h>
47 //#include <vtkGESignalReader.h>
49 #include "boost/filesystem/path.hpp"
54 //=====================================================================
55 ImageReader::ImageReader()
58 mLastFilename("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")
60 // std::cout << "#### ImageReader::ImageReader()"<<std::endl;
61 if (mUnreadableImage!=0) return;
64 Register( boost::shared_ptr<AbstractImageReader>(new VtkImageReader(vtkPNGReader::New() , "PNG", ".png")));
65 Register(boost::shared_ptr<AbstractImageReader>(new VtkImageReader(vtkTIFFReader::New(), "JPEG", ".jpeg")));
66 Register(boost::shared_ptr<AbstractImageReader>(new VtkImageReader(vtkJPEGReader::New())));
67 Register(boost::shared_ptr<AbstractImageReader>(new VtkImageReader(vtkBMPReader::New())));
68 Register(boost::shared_ptr<AbstractImageReader>(new VtkImageReader(vtkSLCReader::New())));
69 Register(boost::shared_ptr<AbstractImageReader>(new VtkImageReader(vtkMetaImageReader::New(),"MHD",".mhd")));
70 // Register(new VtkImageReader(vtkGESignalReader::New()));
71 Register(boost::shared_ptr<AbstractImageReader>(new DicomImageReader));
72 Register(boost::shared_ptr<AbstractImageReader>(new UltrasonixImageReader));
76 mUnreadableImage = vtkImageData::New();
78 dim[0] = dim[1] = 128;
80 mUnreadableImage->SetDimensions ( dim );
81 mUnreadableImage->SetScalarTypeToUnsignedChar();
82 mUnreadableImage->AllocateScalars();
83 for (int i=0;i<dim[0];i++)
84 for (int j=0;j<dim[1];j++)
85 mUnreadableImage->SetScalarComponentFromFloat(i,j,0,0,0);
86 for (int i=0;i<dim[0];i++)
88 mUnreadableImage->SetScalarComponentFromFloat(i,i,0,0,255);
89 mUnreadableImage->SetScalarComponentFromFloat(dim[0]-1-i,i,0,0,255);
92 //=====================================================================
94 //=====================================================================
95 ImageReader::~ImageReader()
98 // for (i=mReader.begin(); i!=mReader.end(); i++)
103 if (mUnreadableImage!=0)
105 mUnreadableImage->Delete();
106 mUnreadableImage = 0;
109 //=====================================================================
111 //=====================================================================
112 void ImageReader::Register(boost::shared_ptr<AbstractImageReader> r)
114 mReader.push_back(r);
118 void ImageReader::UnRegister(const std::string i_val)
120 mUnReader.push_back(i_val);
123 //=====================================================================
125 //=====================================================================
126 // Returns true iff the file is readable
127 bool ImageReader::ShallNotRead( const std::string& filename )
132 std::vector<std::string >::iterator i ;
133 for (i=mUnReader.begin(); i!=mUnReader.end(); i++)
136 if ( (*i).c_str() == filename)
146 //=====================================================================
147 // Returns true iff the file is readable
148 bool ImageReader::CanRead( const std::string& filename )
152 if( !ShallNotRead(filename))
158 std::vector<boost::shared_ptr<AbstractImageReader> >::iterator i;
159 for (i=mReader.begin(); i!=mReader.end(); i++)
161 ok = (*i)->CanRead(filename);
164 mLastFilename = filename;
172 //=====================================================================
174 //=====================================================================
175 // Reads the file (CanRead must be called before : no test here)
176 vtkImageData* ImageReader::ReadImage( const std::string& filename)
178 if (mLastFilename!=filename)
180 if (!CanRead(filename))
182 vtkImageData* im = vtkImageData::New();
183 im->ShallowCopy(mUnreadableImage);
187 vtkImageData* i = mLastReader->ReadImage(mLastFilename);
190 i = vtkImageData::New();
191 i->ShallowCopy(mUnreadableImage);
195 //=====================================================================
196 // Another function to read attributes for a file
197 void ImageReader::getAttributes(const std::string filename,
198 std::map <std::string , std::string> &infos, std::vector<std::string> i_attr)
200 if (mLastFilename!=filename)
202 if (!CanRead(filename))
207 mLastReader->getAttributes(filename, infos, i_attr);
209 //=====================================================================
210 void ImageReader::ReadAttributes(const std::string& filename,
211 std::map<std::string,std::string>& attr)
213 if (mLastFilename!=filename)
215 if (!CanRead(filename))
220 mLastReader->ReadAttributes(mLastFilename,attr);
222 //=====================================================================
225 //=====================================================================
226 /// Pushes back all kwown extensions (without dot) in the vector given
227 void ImageReader::PushBackExtensions(std::vector<std::string>& v)
229 std::vector<boost::shared_ptr<AbstractImageReader> >::iterator i;
230 for (i=mReader.begin(); i!=mReader.end(); i++)
232 (*i)->PushBackExtensions(v);
235 //=====================================================================
237 } // namespace creaImageIO