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 <vtkVersionMacros.h>
30 #include <creaImageIOImageReader.h>
31 #include <creaImageIOTreeAttributeDescriptor.h>
32 #include <creaImageIOSystem.h>
34 #include <creaImageIOVtkImageReader.h>
35 #if defined (USE_GDCM)
36 #include <creaImageIODicomImageReader.h>
38 #if defined(USE_GDCM2)
39 #include <creaImageIODicomImageReader2.h>
41 #include <creaImageIOUltrasonixImageReader.h>
42 #include <vtkPNGReader.h>
43 #include <vtkTIFFReader.h>
44 #include <vtkJPEGReader.h>
45 #include <vtkBMPReader.h>
46 #include <vtkSLCReader.h>
47 #include <vtkMetaImageReader.h>
48 //#include <vtkGESignalReader.h>
50 #include "boost/filesystem/path.hpp"
55 //=====================================================================
56 ImageReader::ImageReader()
58 mUnreadableImage(NULL),
59 mLastFilename("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")
61 // std::cout << "#### ImageReader::ImageReader()"<<std::endl;
62 if (mUnreadableImage!=NULL) return;
65 Register( boost::shared_ptr<AbstractImageReader>(new VtkImageReader(vtkPNGReader::New() , "PNG", ".png")));
66 Register(boost::shared_ptr<AbstractImageReader>(new VtkImageReader(vtkTIFFReader::New(), "TIFF", ".tiff")));
67 Register(boost::shared_ptr<AbstractImageReader>(new VtkImageReader(vtkJPEGReader::New(), "JPEG", ".jpeg")));
68 Register(boost::shared_ptr<AbstractImageReader>(new VtkImageReader(vtkBMPReader::New(), "BMP", ".bmp")));
69 Register(boost::shared_ptr<AbstractImageReader>(new VtkImageReader(vtkSLCReader::New())));
70 Register(boost::shared_ptr<AbstractImageReader>(new VtkImageReader(vtkMetaImageReader::New(),"MHD",".mhd")));
71 // Register(new VtkImageReader(vtkGESignalReader::New()));
72 Register(boost::shared_ptr<AbstractImageReader>(new DicomImageReader));
73 Register(boost::shared_ptr<AbstractImageReader>(new UltrasonixImageReader));
77 mUnreadableImage = vtkImageData::New();
79 dim[0] = dim[1] = 128;
81 mUnreadableImage->SetDimensions ( dim );
83 //EED 2017-01-01 Migration VTK7
84 #if VTK_MAJOR_VERSION <= 5
85 mUnreadableImage->SetScalarTypeToUnsignedChar();
86 mUnreadableImage->AllocateScalars();
88 mUnreadableImage->AllocateScalars(VTK_UNSIGNED_CHAR,1);
91 for (int i=0;i<dim[0];i++)
93 for (int j=0;j<dim[1];j++)
95 mUnreadableImage->SetScalarComponentFromFloat(i,j,0,0,0);
99 for (int i=0;i<dim[0];i++)
101 mUnreadableImage->SetScalarComponentFromFloat(i,i,0,0,255);
102 mUnreadableImage->SetScalarComponentFromFloat(dim[0]-1-i,i,0,0,255);
105 //=====================================================================
107 //=====================================================================
108 ImageReader::~ImageReader()
111 // for (i=mReader.begin(); i!=mReader.end(); i++)
116 if (mUnreadableImage!=NULL)
118 mUnreadableImage->Delete();
119 mUnreadableImage = NULL;
122 //=====================================================================
124 //=====================================================================
125 void ImageReader::Register(boost::shared_ptr<AbstractImageReader> r)
127 mReader.push_back(r);
130 void ImageReader::UnRegister(const std::string i_val)
132 mUnReader.push_back(i_val);
135 //=====================================================================
137 //=====================================================================
138 // Returns true iff the file is readable
139 bool ImageReader::ShallNotRead( const std::string& filename )
144 std::vector<std::string >::iterator i ;
145 for (i=mUnReader.begin(); i!=mUnReader.end(); i++)
147 if ( (*i).c_str() == filename)
157 //=====================================================================
158 // Returns true iff the file is readable
159 bool ImageReader::CanRead( const std::string& filename )
164 if( !ShallNotRead(filename))
170 std::vector<boost::shared_ptr<AbstractImageReader> >::iterator i;
171 for (i=mReader.begin(); i!=mReader.end(); i++)
173 ok = (*i)->CanRead(filename);
176 mLastFilename = filename;
184 //=====================================================================
186 //=====================================================================
187 // Reads the file (CanRead must be called before : no test here)
188 vtkImageData* ImageReader::ReadImage( const std::string& filename)
190 if (mLastFilename!=filename)
192 if (!CanRead(filename))
194 vtkImageData* im = vtkImageData::New();
195 im->ShallowCopy(mUnreadableImage);
198 } // for mLastFilename
199 vtkImageData* i = mLastReader->ReadImage(mLastFilename);
202 i = vtkImageData::New();
203 i->ShallowCopy(mUnreadableImage);
208 //=====================================================================
209 // Another function to read attributes for a file
210 void ImageReader::getAttributes(const std::string filename,
211 std::map <std::string , std::string> &infos, std::vector<std::string> i_attr)
213 if (mLastFilename!=filename)
215 if (!CanRead(filename))
220 mLastReader->getAttributes(filename, infos, i_attr);
222 //=====================================================================
223 void ImageReader::ReadAttributes(const std::string& filename,
224 std::map<std::string,std::string>& attr)
226 if (mLastFilename!=filename)
228 if (!CanRead(filename))
233 mLastReader->ReadAttributes(mLastFilename,attr);
235 //=====================================================================
238 //=====================================================================
239 /// Pushes back all kwown extensions (without dot) in the vector given
240 void ImageReader::PushBackExtensions(std::vector<std::string>& v)
242 std::vector<boost::shared_ptr<AbstractImageReader> >::iterator i;
243 for (i=mReader.begin(); i!=mReader.end(); i++)
245 (*i)->PushBackExtensions(v);
248 //=====================================================================
250 } // namespace creaImageIO