]> Creatis software - creaImageIO.git/blob - src/creaImageIOImageReader.cpp
#3111 creaImageIO Bug New Normal - branch vtk7itk4 compilation with vtk7
[creaImageIO.git] / src / creaImageIOImageReader.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 <creaImageIOImageReader.h>
30 #include <creaImageIOTreeAttributeDescriptor.h>
31 #include <creaImageIOSystem.h>
32
33 #include <creaImageIOVtkImageReader.h>
34 #if defined (USE_GDCM)
35         #include <creaImageIODicomImageReader.h>
36 #endif
37 #if defined(USE_GDCM2)
38         #include <creaImageIODicomImageReader2.h>
39 #endif
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>
48
49 #include "boost/filesystem/path.hpp"
50
51 namespace creaImageIO
52 {
53
54   //=====================================================================
55   ImageReader::ImageReader()
56     :   
57     mUnreadableImage(0),
58     mLastFilename("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")
59   {
60     //    std::cout << "#### ImageReader::ImageReader()"<<std::endl;
61     if (mUnreadableImage!=0) return;
62
63
64         Register( boost::shared_ptr<AbstractImageReader>(new VtkImageReader(vtkPNGReader::New() , "PNG", ".png")));
65     Register(boost::shared_ptr<AbstractImageReader>(new VtkImageReader(vtkTIFFReader::New(), "TIFF", ".tiff")));
66     Register(boost::shared_ptr<AbstractImageReader>(new VtkImageReader(vtkJPEGReader::New(), "JPEG", ".jpeg")));
67     Register(boost::shared_ptr<AbstractImageReader>(new VtkImageReader(vtkBMPReader::New(), "BMP", ".bmp")));
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));
73
74         UnRegister(".txt");
75  
76     mUnreadableImage = vtkImageData::New();
77     int dim[3];
78     dim[0] = dim[1] = 128; 
79     dim[2] = 1; 
80     mUnreadableImage->SetDimensions ( dim );
81
82 //EED 2017-01-01 Migration VTK7
83 #if VTK_MAJOR_VERSION <= 5
84     mUnreadableImage->SetScalarTypeToUnsignedChar();
85     mUnreadableImage->AllocateScalars();   
86 #else
87     mUnreadableImage->AllocateScalars(VTK_UNSIGNED_CHAR,1);
88 #endif
89  
90     for (int i=0;i<dim[0];i++) 
91       for (int j=0;j<dim[1];j++) 
92         mUnreadableImage->SetScalarComponentFromFloat(i,j,0,0,0);
93     for (int i=0;i<dim[0];i++) 
94       {
95         mUnreadableImage->SetScalarComponentFromFloat(i,i,0,0,255);
96         mUnreadableImage->SetScalarComponentFromFloat(dim[0]-1-i,i,0,0,255);
97       }
98   }
99   //=====================================================================
100
101   //=====================================================================
102   ImageReader::~ImageReader()
103   {
104
105  //   for (i=mReader.begin(); i!=mReader.end(); i++)
106  //     {
107         //delete (*i);
108  //     }
109 //    mReader.clear();
110     if (mUnreadableImage!=0) 
111       {
112         mUnreadableImage->Delete();
113         mUnreadableImage = 0;
114       }
115   }
116   //=====================================================================
117
118   //=====================================================================
119   void ImageReader::Register(boost::shared_ptr<AbstractImageReader> r)
120   {
121     mReader.push_back(r);
122
123   }
124
125   void ImageReader::UnRegister(const std::string i_val)
126   {
127     mUnReader.push_back(i_val);
128
129   }
130   //=====================================================================
131
132    //=====================================================================
133   // Returns true iff the file is readable
134   bool ImageReader::ShallNotRead( const std::string& filename ) 
135   {
136     bool ok = true;
137         if(filename != "")
138         {
139                 std::vector<std::string >::iterator i ;
140                 for (i=mUnReader.begin(); i!=mUnReader.end(); i++)
141                 {
142                         
143                         if ( (*i).c_str() == filename) 
144                         {
145                                 ok = false;
146                                 break;
147                         }
148                 }
149         }
150         return ok;
151   }
152
153   //=====================================================================
154   // Returns true iff the file is readable
155   bool ImageReader::CanRead( const std::string& filename ) 
156   {
157     bool ok = false;
158
159         if( !ShallNotRead(filename))
160         {
161                 return ok;
162         }
163         if(filename != "")
164         {
165                 std::vector<boost::shared_ptr<AbstractImageReader> >::iterator i;
166                 for (i=mReader.begin(); i!=mReader.end(); i++)
167                 {
168                         ok = (*i)->CanRead(filename);
169                         if (ok) 
170                         {
171                                 mLastFilename = filename;
172                                 mLastReader = *i;
173                                 break;
174                         }
175                 }
176         }
177         return ok;
178   }
179   //=====================================================================
180
181   //=====================================================================
182   // Reads the file (CanRead must be called before : no test here)
183   vtkImageData*  ImageReader::ReadImage( const std::string& filename)
184   {
185           if (mLastFilename!=filename)
186       {
187         if (!CanRead(filename))  
188           { 
189             vtkImageData* im = vtkImageData::New();
190             im->ShallowCopy(mUnreadableImage);
191             return im;
192           }
193       }
194     vtkImageData* i = mLastReader->ReadImage(mLastFilename);
195     if (i==0) 
196       {
197         i = vtkImageData::New();
198         i->ShallowCopy(mUnreadableImage);
199       }
200     return i;
201   }
202   //=====================================================================
203   // Another function to read attributes for a file
204   void ImageReader::getAttributes(const std::string filename,
205                 std::map <std::string , std::string> &infos, std::vector<std::string> i_attr)
206   {
207            if (mLastFilename!=filename)
208       {
209         if (!CanRead(filename)) 
210           { 
211             return;
212           }
213       }
214     mLastReader->getAttributes(filename, infos, i_attr);
215   }
216   //=====================================================================
217    void ImageReader::ReadAttributes(const std::string& filename, 
218                                     std::map<std::string,std::string>& attr)
219    {
220     if (mLastFilename!=filename)
221       {
222         if (!CanRead(filename)) 
223           { 
224             return;
225           }
226       }
227     mLastReader->ReadAttributes(mLastFilename,attr);
228    }
229   //=====================================================================
230
231
232   //=====================================================================
233   /// Pushes back all kwown extensions (without dot) in the vector given
234   void ImageReader::PushBackExtensions(std::vector<std::string>& v)
235   {
236           std::vector<boost::shared_ptr<AbstractImageReader> >::iterator i;
237     for (i=mReader.begin(); i!=mReader.end(); i++)
238       {
239         (*i)->PushBackExtensions(v);
240       }
241   }
242   //=====================================================================
243   
244 } // namespace creaImageIO