22 #include <itkMultiThreader.h> 23 #include <itkImageFileReader.h> 24 #include <itkImageFileWriter.h> 26 #include <vtkSmartPointer.h> 27 #include <vtkCommand.h> 28 #include <vtkImageData.h> 29 #include <vtkImageFlip.h> 30 #include <vtkPolyData.h> 31 #include <vtkPolyDataCollection.h> 33 #include "itkImageToVTKImageFilter.h" 34 #include "itkVTKImageToImageFilter.h" 36 #include <milxGlobal.h> 38 typedef std::string string;
58 inline const char* GetMessage()
60 return m_Message.c_str();
63 inline bool HasFailed()
65 return m_ErrorEncountered;
68 inline bool ReportsFailure()
70 return m_ErrorEncountered;
75 m_ErrorEncountered = m_WarningEncountered =
false;
82 bool m_ErrorEncountered;
83 bool m_WarningEncountered;
87 void Execute(vtkObject *caller,
unsigned long observedType,
void* message);
137 template<
class Type,
unsigned int Dimension>
138 bool OpenImage(
const string filename,
typename itk::Image<Type, Dimension>::Pointer &data);
146 template<
class Type,
unsigned int Dimension>
147 bool OpenImage(
const string filename, vtkSmartPointer<vtkImageData> &data);
155 template<
class Type,
unsigned int Dimension>
156 bool SaveImage(
const string filename,
typename itk::Image<Type, Dimension>::Pointer data);
164 template<
class Type,
unsigned int Dimension>
165 bool SaveImage(
const string filename, vtkSmartPointer<vtkImageData> data);
174 static bool OpenModel(
const string filename, vtkSmartPointer<vtkPolyData> &data);
181 static bool OpenModelCollection(std::vector<string> filenames, vtkSmartPointer<vtkPolyDataCollection> &collection);
188 static bool SaveModel(
const string filename, vtkSmartPointer<vtkPolyData> data,
const bool binary =
false);
195 static bool SaveModelCollection(std::vector<string> filenames, vtkSmartPointer<vtkPolyDataCollection> collection);
201 static inline string StringToLowerCase(
string filename)
203 std::transform(filename.begin(), filename.end(), filename.begin(), ::tolower);
212 static inline string StripFileExtension(
const string &filename)
214 return filename.substr(0, filename.find_last_of(
"."));
222 static inline string ExtractFilename(
const string &filename,
char delimiter =
'/')
224 return filename.substr( filename.find_last_of( delimiter ) + 1 );
232 static inline string ExtractPath(
const string &filename,
char delimiter =
'/')
234 return filename.substr( 0, filename.find_last_of( delimiter ) + 1 );
242 static inline string GetBaseName(
const string &filename)
244 return StripFileExtension( ExtractFilename(filename) );
252 static inline string GetFileExtension(
const string &filename)
254 if (filename.find_last_of(
".") != string::npos)
255 return StringToLowerCase( filename.substr(filename.find_last_of(
".")+1) );
266 typename itk::ProcessObject::Pointer Filter;
272 static ITK_THREAD_RETURN_TYPE ThreaderCallback(
void *arg );
281 ITK_THREAD_RETURN_TYPE File::ThreaderCallback(
void *arg )
285 str = (
ThreadStruct *)(((itk::MultiThreader::ThreadInfoStruct *)(arg))->UserData);
289 str->Filter->Update();
291 catch( itk::ExceptionObject & err )
297 return ITK_THREAD_RETURN_VALUE;
300 template<
class Type,
unsigned int Dimension>
301 bool File::OpenImage(
const string filename,
typename itk::Image<Type, Dimension>::Pointer &data)
303 typedef itk::ImageFileReader< itk::Image<Type, Dimension> > ImageReader;
308 typename ImageReader::Pointer reader = ImageReader::New();
309 reader->SetFileName(filename.c_str());
310 threadData.Filter = reader;
313 typedef itk::MultiThreader ThreaderType;
315 ThreaderType::Pointer threader = ThreaderType::New();
316 threader->SetNumberOfThreads( 1 );
317 threader->SetSingleMethod( ThreaderCallback, &threadData );
318 threader->SingleMethodExecute();
320 data = reader->GetOutput();
325 template<
class Type,
unsigned int Dimension>
326 bool File::OpenImage(
const string filename, vtkSmartPointer<vtkImageData> &data)
328 typename itk::Image<Type, Dimension>::Pointer tmpImage;
330 if( !OpenImage(filename, tmpImage) )
333 typedef itk::ImageToVTKImageFilter< itk::Image<Type, Dimension> > ConnectorType;
335 typename ConnectorType::Pointer connector = ConnectorType::New();
336 connector->SetInput(tmpImage);
340 vtkSmartPointer<vtkImageFlip> imageReorient = vtkSmartPointer<vtkImageFlip>::New();
341 imageReorient->SetInput(connector->GetOutput());
342 imageReorient->SetFilteredAxis(1);
343 imageReorient->FlipAboutOriginOn();
344 imageReorient->Update();
346 data = imageReorient->GetOutput();
351 template<
class Type,
unsigned int Dimension>
352 bool File::SaveImage(
const string filename,
typename itk::Image<Type, Dimension>::Pointer data)
354 typedef itk::ImageFileWriter< itk::Image<Type, Dimension> > ImageWriter;
359 typename ImageWriter::Pointer writer = ImageWriter::New();
360 writer->SetFileName(filename.c_str());
361 threadData.Filter = writer;
364 typedef itk::MultiThreader ThreaderType;
366 ThreaderType::Pointer threader = ThreaderType::New();
367 threader->SetNumberOfThreads( 1 );
368 threader->SetSingleMethod( ThreaderCallback, &threadData );
369 threader->SingleMethodExecute();
374 template<
class Type,
unsigned int Dimension>
375 bool File::SaveImage(
const string filename, vtkSmartPointer<vtkImageData> data)
378 vtkSmartPointer<vtkImageFlip> imageReorient = vtkSmartPointer<vtkImageFlip>::New();
379 imageReorient->SetInput(data);
380 imageReorient->SetFilteredAxis(1);
381 imageReorient->FlipAboutOriginOn();
382 imageReorient->Update();
384 typedef itk::VTKImageToImageFilter< itk::Image<Type, Dimension> > ConnectorType;
386 typename ConnectorType::Pointer connector = ConnectorType::New();
387 connector->SetInput(imageReorient->GetOutput());
390 if( !SaveImage(filename, connector->GetOutput()) )
398 #endif //__MILXFILE_H bool OpenImage(const string filename, typename itk::Image< Type, Dimension >::Pointer &data)
Opens an image file, which is any of the following: JPEG, PNG, DICOM, TIFF, NIFTI etc...
void PrintError(const std::string msg)
Displays a generic msg to standard error with carriage return.
Object for intercepting errors thrown by VTK based readers.
Thread Struct for running IO classes in threads.
#define SMILI_EXPORT
DLL Function Symbol for Windows. It is empty for other OSes.
A general file IO class for reading writing images and models.