creaImageIO_lib
creaImageIOImageReader.cpp
Go to the documentation of this file.
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>
31 #include <creaImageIOSystem.h>
32 
34 #if defined (USE_GDCM)
36 #endif
37 #if defined(USE_GDCM2)
39 #endif
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  //=====================================================================
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  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++)
87  {
88  mUnreadableImage->SetScalarComponentFromFloat(i,i,0,0,255);
89  mUnreadableImage->SetScalarComponentFromFloat(dim[0]-1-i,i,0,0,255);
90  }
91  }
92  //=====================================================================
93 
94  //=====================================================================
96  {
97 
98  // for (i=mReader.begin(); i!=mReader.end(); i++)
99  // {
100  //delete (*i);
101  // }
102 // mReader.clear();
103  if (mUnreadableImage!=0)
104  {
105  mUnreadableImage->Delete();
106  mUnreadableImage = 0;
107  }
108  }
109  //=====================================================================
110 
111  //=====================================================================
112  void ImageReader::Register(boost::shared_ptr<AbstractImageReader> r)
113  {
114  mReader.push_back(r);
115 
116  }
117 
118  void ImageReader::UnRegister(const std::string i_val)
119  {
120  mUnReader.push_back(i_val);
121 
122  }
123  //=====================================================================
124 
125  //=====================================================================
126  // Returns true iff the file is readable
127  bool ImageReader::ShallNotRead( const std::string& filename )
128  {
129  bool ok = true;
130  if(filename != "")
131  {
132  std::vector<std::string >::iterator i ;
133  for (i=mUnReader.begin(); i!=mUnReader.end(); i++)
134  {
135 
136  if ( (*i).c_str() == filename)
137  {
138  ok = false;
139  break;
140  }
141  }
142  }
143  return ok;
144  }
145 
146  //=====================================================================
147  // Returns true iff the file is readable
148  bool ImageReader::CanRead( const std::string& filename )
149  {
150  bool ok = false;
151 
152  if( !ShallNotRead(filename))
153  {
154  return ok;
155  }
156  if(filename != "")
157  {
158  std::vector<boost::shared_ptr<AbstractImageReader> >::iterator i;
159  for (i=mReader.begin(); i!=mReader.end(); i++)
160  {
161  ok = (*i)->CanRead(filename);
162  if (ok)
163  {
164  mLastFilename = filename;
165  mLastReader = *i;
166  break;
167  }
168  }
169  }
170  return ok;
171  }
172  //=====================================================================
173 
174  //=====================================================================
175  // Reads the file (CanRead must be called before : no test here)
176  vtkImageData* ImageReader::ReadImage( const std::string& filename)
177  {
178  if (mLastFilename!=filename)
179  {
180  if (!CanRead(filename))
181  {
182  vtkImageData* im = vtkImageData::New();
183  im->ShallowCopy(mUnreadableImage);
184  return im;
185  }
186  }
187  vtkImageData* i = mLastReader->ReadImage(mLastFilename);
188  if (i==0)
189  {
190  i = vtkImageData::New();
191  i->ShallowCopy(mUnreadableImage);
192  }
193  return i;
194  }
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)
199  {
200  if (mLastFilename!=filename)
201  {
202  if (!CanRead(filename))
203  {
204  return;
205  }
206  }
207  mLastReader->getAttributes(filename, infos, i_attr);
208  }
209  //=====================================================================
210  void ImageReader::ReadAttributes(const std::string& filename,
211  std::map<std::string,std::string>& attr)
212  {
213  if (mLastFilename!=filename)
214  {
215  if (!CanRead(filename))
216  {
217  return;
218  }
219  }
220  mLastReader->ReadAttributes(mLastFilename,attr);
221  }
222  //=====================================================================
223 
224 
225  //=====================================================================
227  void ImageReader::PushBackExtensions(std::vector<std::string>& v)
228  {
229  std::vector<boost::shared_ptr<AbstractImageReader> >::iterator i;
230  for (i=mReader.begin(); i!=mReader.end(); i++)
231  {
232  (*i)->PushBackExtensions(v);
233  }
234  }
235  //=====================================================================
236 
237 } // namespace creaImageIO