SMILX  1.01
milxFile_Threaded.h
1 /*=========================================================================
2  Copyright: (c) CSIRO, Australia
3 
4  This software is protected by international copyright laws.
5  Any unauthorised copying, distribution or reverse engineering is prohibited.
6 
7  Licence:
8  All rights in this Software are reserved to CSIRO. You are only permitted
9  to have this Software in your possession and to make use of it if you have
10  agreed to a Software License with CSIRO.
11 =========================================================================*/
12 
13 #ifndef __MILXFILE_H
14 #define __MILXFILE_H
15 
16 //STL
17 #include <string>
18 #include <algorithm>
19 #include <vector>
20 //ITK
21 #include <itkImage.h>
22 #include <itkMultiThreader.h>
23 #include <itkImageFileReader.h>
24 #include <itkImageFileWriter.h>
25 //VTK
26 #include <vtkSmartPointer.h>
27 #include <vtkCommand.h>
28 #include <vtkImageData.h>
29 #include <vtkImageFlip.h>
30 #include <vtkPolyData.h>
31 #include <vtkPolyDataCollection.h>
32 //MILX
33 #include "itkImageToVTKImageFilter.h"
34 #include "itkVTKImageToImageFilter.h"
35 //SMILI
36 #include <milxGlobal.h>
37 
38 typedef std::string string; //Don't open std namespace
39 
44 class VTK_COMMON_EXPORT vtkErrorWarning : public vtkCommand
45 {
46 public:
51  static vtkErrorWarning* New()
52  {
53  return new vtkErrorWarning();
54  }
55 
56  vtkTypeMacro(vtkErrorWarning, vtkCommand);
57 
58  inline const char* GetMessage()
59  {
60  return m_Message.c_str();
61  }
62 
63  inline bool HasFailed()
64  {
65  return m_ErrorEncountered;
66  }
67 
68  inline bool ReportsFailure()
69  {
70  return m_ErrorEncountered;
71  }
72 
73  void Initialize()
74  {
75  m_ErrorEncountered = m_WarningEncountered = false;
76  }
77 
78 protected:
80  ~vtkErrorWarning();
81 
82  bool m_ErrorEncountered;
83  bool m_WarningEncountered;
84  string m_Message;
85 
86 private:
87  void Execute(vtkObject *caller, unsigned long observedType, void* message);
88 };
89 
90 namespace milx
91 {
92 
113 //template<class Type = float>
114 class SMILI_EXPORT File
115 {
116 public:
121  File() {};
126  ~File() {};
127 
128 public:
129  //Images
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);
166 
167  //Surfaces/Models
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);
196 
201  static inline string StringToLowerCase(string filename)
202  {
203  std::transform(filename.begin(), filename.end(), filename.begin(), ::tolower);
204  return filename;
205  }
212  static inline string StripFileExtension(const string &filename)
213  {
214  return filename.substr(0, filename.find_last_of("."));
215  }
222  static inline string ExtractFilename(const string &filename, char delimiter = '/')
223  {
224  return filename.substr( filename.find_last_of( delimiter ) + 1 );
225  }
232  static inline string ExtractPath(const string &filename, char delimiter = '/')
233  {
234  return filename.substr( 0, filename.find_last_of( delimiter ) + 1 );
235  }
242  static inline string GetBaseName(const string &filename)
243  {
244  return StripFileExtension( ExtractFilename(filename) );
245  }
252  static inline string GetFileExtension(const string &filename)
253  {
254  if (filename.find_last_of(".") != string::npos)
255  return StringToLowerCase( filename.substr(filename.find_last_of(".")+1) );
256  return "";
257  }
258 
259 protected:
265  {
266  typename itk::ProcessObject::Pointer Filter;
267  };
268 
272  static ITK_THREAD_RETURN_TYPE ThreaderCallback( void *arg );
273 
274 private:
275 
276 };
277 
278 // Callback routine used by the threading library. This routine just calls
279 // the ThreadedGenerateData method after setting the correct region for this
280 // thread.
281 ITK_THREAD_RETURN_TYPE File::ThreaderCallback( void *arg )
282 {
283  ThreadStruct *str;
284 
285  str = (ThreadStruct *)(((itk::MultiThreader::ThreadInfoStruct *)(arg))->UserData);
286 
287  try
288  {
289  str->Filter->Update();
290  }
291  catch( itk::ExceptionObject & err )
292  {
293  PrintError("File Exception caught!");
294  err.Print(cerr);
295  }
296 
297  return ITK_THREAD_RETURN_VALUE;
298 }
299 
300 template<class Type, unsigned int Dimension>
301 bool File::OpenImage(const string filename, typename itk::Image<Type, Dimension>::Pointer &data)
302 {
303  typedef itk::ImageFileReader< itk::Image<Type, Dimension> > ImageReader;
304 
306  ThreadStruct threadData;
307 
308  typename ImageReader::Pointer reader = ImageReader::New();
309  reader->SetFileName(filename.c_str());
310  threadData.Filter = reader;
311 
312  //Threading
313  typedef itk::MultiThreader ThreaderType;
314 
315  ThreaderType::Pointer threader = ThreaderType::New();
316  threader->SetNumberOfThreads( 1 );
317  threader->SetSingleMethod( ThreaderCallback, &threadData );
318  threader->SingleMethodExecute();
319 
320  data = reader->GetOutput();
321 
322  return true;
323 }
324 
325 template<class Type, unsigned int Dimension>
326 bool File::OpenImage(const string filename, vtkSmartPointer<vtkImageData> &data)
327 {
328  typename itk::Image<Type, Dimension>::Pointer tmpImage;
329 
330  if( !OpenImage(filename, tmpImage) )
331  return false;
332 
333  typedef itk::ImageToVTKImageFilter< itk::Image<Type, Dimension> > ConnectorType;
335  typename ConnectorType::Pointer connector = ConnectorType::New();
336  connector->SetInput(tmpImage);
337  connector->Update();
338 
340  vtkSmartPointer<vtkImageFlip> imageReorient = vtkSmartPointer<vtkImageFlip>::New();
341  imageReorient->SetInput(connector->GetOutput());
342  imageReorient->SetFilteredAxis(1);
343  imageReorient->FlipAboutOriginOn();
344  imageReorient->Update();
345 
346  data = imageReorient->GetOutput();
347 
348  return true;
349 }
350 
351 template<class Type, unsigned int Dimension>
352 bool File::SaveImage(const string filename, typename itk::Image<Type, Dimension>::Pointer data)
353 {
354  typedef itk::ImageFileWriter< itk::Image<Type, Dimension> > ImageWriter;
355 
357  ThreadStruct threadData;
358 
359  typename ImageWriter::Pointer writer = ImageWriter::New();
360  writer->SetFileName(filename.c_str());
361  threadData.Filter = writer;
362 
363  //Threading
364  typedef itk::MultiThreader ThreaderType;
365 
366  ThreaderType::Pointer threader = ThreaderType::New();
367  threader->SetNumberOfThreads( 1 );
368  threader->SetSingleMethod( ThreaderCallback, &threadData );
369  threader->SingleMethodExecute();
370 
371  return true;
372 }
373 
374 template<class Type, unsigned int Dimension>
375 bool File::SaveImage(const string filename, vtkSmartPointer<vtkImageData> data)
376 {
378  vtkSmartPointer<vtkImageFlip> imageReorient = vtkSmartPointer<vtkImageFlip>::New();
379  imageReorient->SetInput(data);
380  imageReorient->SetFilteredAxis(1);
381  imageReorient->FlipAboutOriginOn();
382  imageReorient->Update();
383 
384  typedef itk::VTKImageToImageFilter< itk::Image<Type, Dimension> > ConnectorType;
386  typename ConnectorType::Pointer connector = ConnectorType::New();
387  connector->SetInput(imageReorient->GetOutput());
388  connector->Update();
389 
390  if( !SaveImage(filename, connector->GetOutput()) )
391  return false;
392 
393  return true;
394 }
395 
396 } //end namespace milx
397 
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.
Definition: milxGlobal.h:202
Object for intercepting errors thrown by VTK based readers.
Definition: milxGlobal.h:351
Thread Struct for running IO classes in threads.
#define SMILI_EXPORT
DLL Function Symbol for Windows. It is empty for other OSes.
Definition: milxGlobal.h:227
A general file IO class for reading writing images and models.
Definition: milxFile.h:92