]> Creatis software - creaImageIO.git/blob - src/creaImageIOVtkImageReader.cpp
Clean code
[creaImageIO.git] / src / creaImageIOVtkImageReader.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 <creaImageIOVtkImageReader.h>
30 #include <vtkImageReader2.h>
31
32 #include "boost/filesystem/path.hpp"
33 #if defined(_WIN32)
34 #pragma warning(disable: 4996)
35 #endif
36
37 namespace creaImageIO{
38   //=====================================================================
39   VtkImageReader::VtkImageReader(vtkImageReader2* r, 
40                                        const std::string& name,
41                                        const std::string& extensions)
42     : mReader(r), mExtensions(extensions)
43   {
44           
45         //EED 21 mars 2012  FLIP probleme  ..PLOP..
46         mReader->FileLowerLeftOff();
47           
48     if (name.size() == 0) 
49       {
50                   const char *test =mReader->GetDescriptiveName();
51                   //warning: comparison with string literal results in unspecified behaviour
52                   //if(test != "")
53                   std::string emptyString("");
54                   if(test != emptyString)
55                   {
56                         SetName ( "toto");// mReader->GetDescriptiveName());
57                   }
58       }
59     else 
60       {
61         SetName ( name );
62       }
63
64       
65   }
66   //=====================================================================
67   
68   //=====================================================================
69   VtkImageReader::~VtkImageReader()
70   {
71
72     mReader->Delete();
73   }
74   //=====================================================================
75
76   //=====================================================================
77   bool VtkImageReader::CanRead(const std::string& filename)
78   { 
79
80     return (mReader->CanReadFile(filename.c_str())!=0);
81 /*        if(filename != "")
82           {
83                 return (mReader->CanReadFile(filename.c_str())!=0);
84           }
85           else
86           {
87                   return false;
88           }*/
89   }
90   //=====================================================================
91   void VtkImageReader::getAttributes(const std::string filename,
92                 std::map <std::string , std::string> &infos, std::vector<std::string> i_attr)
93    {
94    }
95   //=====================================================================
96   vtkImageData* VtkImageReader::ReadImage(const std::string& filename)
97   {
98     vtkImageData* im = 0;
99     try
100       {
101         mReader->SetFileName( filename.c_str() );
102         mReader->Update();
103         im = vtkImageData::New();
104
105         im->ShallowCopy(mReader->GetOutput());
106
107 printf("EED VtkImageReader::ReadImage line 108   Missing FlipImage for JPEG, PNG, etc\n");
108 /*
109         im=FlipImageY(im);          
110
111         if ( (GetName()=="JPEG") || (GetName()=="PNG") )
112         {
113
114                 im->Update();
115                 int inputdims[3];
116                 im->GetDimensions (inputdims);
117
118                 int nbScalComp  = im->GetNumberOfScalarComponents();
119                 int scalarSize  = im->GetScalarSize();
120                 int lineSize    = inputdims[0]*scalarSize*nbScalComp;      
121                 int planeSize   = inputdims[1]*lineSize;
122                 int volumeSize  = inputdims[2]*planeSize;
123                 char *pixelsIn  = (char *)im->GetScalarPointer();
124                 char *pixelsOut = (char *)mImageOut->GetScalarPointer();
125         
126                 char *lineIn;
127                 char *lineOut;
128                 char *debPlanIn;
129                 char *debPlanOut;
130                 int i,j,k;
131
132                 for(k=0; k<inputdims[2]; k++)  // iterate  planes
133                         {  
134                                 debPlanIn       = pixelsIn+k*planeSize;
135                                 debPlanOut      = pixelsOut+k*planeSize;
136                                 for(j=0; j<inputdims[1]; j++)  // iterates  rows
137                                 { 
138                                         lineIn = debPlanIn+j*lineSize;
139                                         lineOut = debPlanOut+(inputdims[1]-1-j)*lineSize;
140                                         memcpy(lineOut,  lineIn, lineSize);
141                                 }       // for j
142                         } // for k
143         } // FLIP : JPEG PNG
144 */
145       }
146     catch (...)
147       {
148         if (im!=0) im->Delete();
149         im = 0;
150       }
151     return im;
152   }
153   //=====================================================================
154   
155   //=====================================================================
156   void SplitExtensionsString ( const std::string& str, 
157                                const std::string& delimiters, 
158                                std::vector<std::string>& tokens)
159   {
160     // Skip delimiters at beginning.
161     std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
162     // Find first delimiter.
163     std::string::size_type pos     = str.find_first_of(delimiters, lastPos);
164     
165     while (std::string::npos != pos || std::string::npos != lastPos)
166       {
167         // Found a token, add it to the vector.
168         // SPECIFIC : REMOVE INITIAL DOT (lastPos + 1)
169         tokens.push_back(str.substr(lastPos+1, pos - lastPos));
170         // Skip delimiters.  Note the "not_of"
171         lastPos = str.find_first_not_of(delimiters, pos);
172         // Find next delimiter
173         pos = str.find_first_of(delimiters, lastPos);
174       }
175     
176     }
177   //=====================================================================
178   
179   //=====================================================================
180   void VtkImageReader::PushBackExtensions(std::vector<std::string>& v)
181   {
182     std::string ext = mExtensions;
183     if (ext.size()==0) ext = mReader->GetFileExtensions ();
184     
185     SplitExtensionsString(ext," ",v);
186   }
187   //=====================================================================
188  
189
190
191   //=====================================================================
192   void VtkImageReader::ReadAttributes(const std::string& filename, 
193                                       std::map<std::string,std::string>& attr)
194   {
195
196     // Get image dimensions
197     // How to get the image info without loading it in vtk ?
198     mReader->SetFileName(filename.c_str());
199     mReader->Update(); //OpenFile();
200     int ext[6];
201     mReader->GetDataExtent(ext);
202     // Columns
203     char cols[128];
204     sprintf(cols,"%i",ext[1]-ext[0]);
205     // Rows
206     char rows[128];
207     sprintf(rows,"%i",ext[3]-ext[2]);
208     // Planes 
209     char planes[128];
210     sprintf(planes,"%i",ext[5]-ext[4]);
211    
212         std::map<std::string,std::string>::iterator i;
213     if ( (i = attr.find("FullFileName")) != attr.end())
214       {
215         i->second = filename;
216       }
217     if ( (i = attr.find("D0004_1500")) != attr.end())
218       {
219         boost::filesystem::path full_path(filename);
220         std::string f = full_path.leaf().string();
221         i->second = f;
222       }
223     if ( (i = attr.find("D0028_0010")) != attr.end())
224       {
225         i->second = rows;
226       }
227     if ( (i = attr.find("D0028_0011")) != attr.end())
228       {
229         i->second = cols;
230       }
231     
232     if ( (i = attr.find("D0028_0012")) != attr.end())
233       {
234                 i->second = planes;
235       }
236           if ( (i = attr.find("FullFileDirectory")) != attr.end())
237       {
238          std::string::size_type last_pos = filename.find_last_of("//");
239                  i->second = filename.substr(0, last_pos);
240           }
241
242
243   }
244   //=====================================================================
245
246 } // namespace creaImageIO