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