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