2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
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
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.
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
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 # ------------------------------------------------------------------------
29 #include <creaImageIOVtkImageReader.h>
30 #include <vtkImageReader2.h>
32 #include "boost/filesystem/path.hpp"
34 #pragma warning(disable: 4996)
37 namespace creaImageIO{
38 //=====================================================================
39 VtkImageReader::VtkImageReader(vtkImageReader2* r,
40 const std::string& name,
41 const std::string& extensions)
42 : mReader(r), mExtensions(extensions)
45 //EED 21 mars 2012 FLIP probleme ..PLOP..
46 mReader->FileLowerLeftOff();
50 const char *test =mReader->GetDescriptiveName();
51 //warning: comparison with string literal results in unspecified behaviour
53 std::string emptyString("");
54 if(test != emptyString)
56 SetName ( "toto");// mReader->GetDescriptiveName());
66 //=====================================================================
68 //=====================================================================
69 VtkImageReader::~VtkImageReader()
74 //=====================================================================
76 //=====================================================================
77 bool VtkImageReader::CanRead(const std::string& filename)
80 return (mReader->CanReadFile(filename.c_str())!=0);
83 return (mReader->CanReadFile(filename.c_str())!=0);
90 //=====================================================================
91 void VtkImageReader::getAttributes(const std::string filename,
92 std::map <std::string , std::string> &infos, std::vector<std::string> i_attr)
95 //=====================================================================
96 vtkImageData* VtkImageReader::ReadImage(const std::string& filename)
101 printf("EED VtkImageReader::ReadImage Name:%s\n", GetName().c_str() );
102 mReader->SetFileName( filename.c_str() );
104 im = vtkImageData::New();
106 im->ShallowCopy(mReader->GetOutput());
109 printf("EED ......\n");
110 printf("EED ......\n");
111 printf("EED VtkImageReader::ReadImage line 108 Missing FlipImage for JPEG, PNG, etc\n");
112 printf("EED ......\n");
113 printf("EED ......\n");
117 if ( (GetName()=="JPEG") || (GetName()=="PNG") )
122 im->GetDimensions (inputdims);
124 int nbScalComp = im->GetNumberOfScalarComponents();
125 int scalarSize = im->GetScalarSize();
126 int lineSize = inputdims[0]*scalarSize*nbScalComp;
127 int planeSize = inputdims[1]*lineSize;
128 int volumeSize = inputdims[2]*planeSize;
129 char *pixelsIn = (char *)im->GetScalarPointer();
130 char *pixelsOut = (char *)mImageOut->GetScalarPointer();
138 for(k=0; k<inputdims[2]; k++) // iterate planes
140 debPlanIn = pixelsIn+k*planeSize;
141 debPlanOut = pixelsOut+k*planeSize;
142 for(j=0; j<inputdims[1]; j++) // iterates rows
144 lineIn = debPlanIn+j*lineSize;
145 lineOut = debPlanOut+(inputdims[1]-1-j)*lineSize;
146 memcpy(lineOut, lineIn, lineSize);
154 if (im!=0) im->Delete();
159 //=====================================================================
161 //=====================================================================
162 void SplitExtensionsString ( const std::string& str,
163 const std::string& delimiters,
164 std::vector<std::string>& tokens)
166 // Skip delimiters at beginning.
167 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
168 // Find first delimiter.
169 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
171 while (std::string::npos != pos || std::string::npos != lastPos)
173 // Found a token, add it to the vector.
174 // SPECIFIC : REMOVE INITIAL DOT (lastPos + 1)
175 tokens.push_back(str.substr(lastPos+1, pos - lastPos));
176 // Skip delimiters. Note the "not_of"
177 lastPos = str.find_first_not_of(delimiters, pos);
178 // Find next delimiter
179 pos = str.find_first_of(delimiters, lastPos);
183 //=====================================================================
185 //=====================================================================
186 void VtkImageReader::PushBackExtensions(std::vector<std::string>& v)
188 std::string ext = mExtensions;
189 if (ext.size()==0) ext = mReader->GetFileExtensions ();
191 SplitExtensionsString(ext," ",v);
193 //=====================================================================
197 //=====================================================================
198 void VtkImageReader::ReadAttributes(const std::string& filename,
199 std::map<std::string,std::string>& attr)
202 // Get image dimensions
203 // How to get the image info without loading it in vtk ?
204 mReader->SetFileName(filename.c_str());
205 mReader->Update(); //OpenFile();
207 mReader->GetDataExtent(ext);
210 sprintf(cols,"%i",ext[1]-ext[0]);
213 sprintf(rows,"%i",ext[3]-ext[2]);
216 sprintf(planes,"%i",ext[5]-ext[4]);
218 std::map<std::string,std::string>::iterator i;
219 if ( (i = attr.find("FullFileName")) != attr.end())
221 i->second = filename;
223 if ( (i = attr.find("D0004_1500")) != attr.end())
225 boost::filesystem::path full_path(filename);
226 std::string f = full_path.leaf().string();
229 if ( (i = attr.find("D0028_0010")) != attr.end())
233 if ( (i = attr.find("D0028_0011")) != attr.end())
238 if ( (i = attr.find("D0028_0012")) != attr.end())
242 if ( (i = attr.find("FullFileDirectory")) != attr.end())
244 std::string::size_type last_pos = filename.find_last_of("//");
245 i->second = filename.substr(0, last_pos);
250 //=====================================================================
252 } // namespace creaImageIO