]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Plugins/IO/ImageWriter.cxx
234c916f99f5955407294607dc9edd948ebc1eeb
[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   itk::DataObject* itk_image = NULL;
46   std::string r = "";
47   cpPlugins_Image_Demangle_AllTypes( D, image, itk_image, r, _RealGD );
48   else r = "ImageWriter: Input image type not supported.";
49   return( r );
50 }
51
52 // -------------------------------------------------------------------------
53 template< unsigned int D >
54 std::string cpPlugins::IO::ImageWriter::
55 _GD0_VectorImage( )
56 {
57   auto image = this->GetInputData< cpPlugins::Interface::Image >( "Input" );
58   itk::DataObject* itk_image = NULL;
59   std::string r = "";
60   cpPlugins_VectorImage_Demangle_AllTypes( D, image, itk_image, r, _RealGD );
61   else r = "ImageWriter: Input image type not supported.";
62   return( r );
63 }
64
65 // -------------------------------------------------------------------------
66 template< class I >
67 std::string cpPlugins::IO::ImageWriter::
68 _RealGD( itk::DataObject* image )
69 {
70   typedef itk::ImageFileWriter< I > _W;
71   _W* writer = this->_CreateITK< _W >( );
72   writer->SetFileName( this->m_Parameters->GetSaveFileName( "FileName" ) );
73   writer->SetInput( dynamic_cast< I* >( image ) );
74   try
75   {
76     writer->Update( );
77   }
78   catch( itk::ExceptionObject& err )
79   {
80     return( "ImageWriter: " + std::string( err.GetDescription( ) ) );
81
82   } // yrt
83   return( "" );
84 }
85
86 // eof - $RCSfile$