]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Plugins/IO/ImageWriter.cxx
...
[cpPlugins.git] / lib / cpPlugins / Plugins / IO / ImageWriter.cxx
1 #include "ImageWriter.h"
2 #include <cpPlugins/Interface/Image.h>
3
4 #include <itkImageFileWriter.h>
5
6 // -------------------------------------------------------------------------
7 cpPlugins::IO::ImageWriter::
8 ImageWriter( )
9   : Superclass( )
10 {
11   this->_AddInput( "Input" );
12   this->m_Parameters->ConfigureAsSaveFileName( "FileName" );
13   this->m_Parameters->SetAcceptedFileExtensions(
14     "FileName",
15     "Image files (*.bmp *.png *.jpg *.jpeg *.dcm *.mhd *.nhdr *.nrrd *.tiff)"
16     );
17 }
18
19 // -------------------------------------------------------------------------
20 cpPlugins::IO::ImageWriter::
21 ~ImageWriter( )
22 {
23 }
24
25 // -------------------------------------------------------------------------
26 std::string cpPlugins::IO::ImageWriter::
27 _GenerateData( )
28 {
29   // Thank you very much Microsoft !!! :-@
30   std::string   r = this->_GD0_Image< 2 >( );
31   if( r != "" ) r = this->_GD0_Image< 3 >( );
32   if( r != "" ) r = this->_GD0_Image< 4 >( );
33   if( r != "" ) r = this->_GD0_VectorImage< 2 >( );
34   if( r != "" ) r = this->_GD0_VectorImage< 3 >( );
35   if( r != "" ) r = this->_GD0_VectorImage< 4 >( );
36   return( r );
37 }
38
39 // -------------------------------------------------------------------------
40 template< unsigned int D >
41 std::string cpPlugins::IO::ImageWriter::
42 _GD0_Image( )
43 {
44   auto image = this->GetInputData< cpPlugins::Interface::Image >( "Input" );
45   if( image == NULL )
46     return( "ImageWriter: No input image." );
47
48   itk::DataObject* itk_image = NULL;
49   std::string r = "";
50   cpPlugins_Image_Demangle_AllTypes( D, image, itk_image, r, _RealGD );
51   else r = "ImageWriter: Input image type not supported.";
52
53   return( r );
54 }
55
56 // -------------------------------------------------------------------------
57 template< unsigned int D >
58 std::string cpPlugins::IO::ImageWriter::
59 _GD0_VectorImage( )
60 {
61   auto image = this->GetInputData< cpPlugins::Interface::Image >( "Input" );
62   if( image == NULL )
63     return( "ImageWriter: No input image." );
64
65   itk::DataObject* itk_image = NULL;
66   std::string r = "";
67   cpPlugins_VectorImage_Demangle_AllTypes( D, image, itk_image, r, _RealGD );
68   else r = "ImageWriter: Input image type not supported.";
69
70   return( r );
71 }
72
73 // -------------------------------------------------------------------------
74 template< class I >
75 std::string cpPlugins::IO::ImageWriter::
76 _RealGD( itk::DataObject* image )
77 {
78   // Get filename
79   std::string fname = this->m_Parameters->GetSaveFileName( "FileName" );
80
81   typedef itk::ImageFileWriter< I > _W;
82   _W* writer = this->_CreateITK< _W >( );
83   writer->SetFileName( fname );
84   writer->SetInput( dynamic_cast< I* >( image ) );
85   try
86   {
87     writer->Update( );
88   }
89   catch( itk::ExceptionObject& err )
90   {
91     return( "ImageWriter: " + std::string( err.GetDescription( ) ) );
92
93   } // yrt
94   return( "" );
95 }
96
97 // eof - $RCSfile$