]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Plugins/IO/ImageWriter.cxx
Merge branch 'master' of ssh://git.creatis.insa-lyon.fr/cpPlugins
[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 #ifdef cpPlugins_Interface_QT4
7 #include <QFileDialog>
8 #endif // cpPlugins_Interface_QT4
9
10 // -------------------------------------------------------------------------
11 cpPlugins::IO::ImageWriter::
12 DialogResult cpPlugins::IO::ImageWriter::
13 ExecConfigurationDialog( QWidget* parent )
14 {
15   DialogResult r = Self::DialogResult_Cancel;
16
17 #ifdef cpPlugins_Interface_QT4
18
19   std::string name = this->m_Parameters->GetString( "FileName" );
20   if( name == "" )
21     name = "save.mhd";
22
23   // Show dialog and check if it was accepted
24   QString qname =
25     QFileDialog::getSaveFileName(
26       parent,
27       QFileDialog::tr( "Save File" ),
28       QFileDialog::tr( name.c_str( ) ),
29       QFileDialog::tr( "Image files (*.bmp *.png *.jpg *.jpeg *.dcm *.mhd *.nhdr *.nrrd *.tiff);;Any file (*)")
30       );
31   name = qname.toStdString( );
32   if( name != "" )
33   {
34     this->m_Parameters->SetString( "FileName", name );
35     r = Self::DialogResult_NoModal;
36
37   } // fi
38
39 #endif // cpPlugins_Interface_QT4
40
41   return( r );
42 }
43
44 // -------------------------------------------------------------------------
45 cpPlugins::IO::ImageWriter::
46 ImageWriter( )
47   : Superclass( )
48 {
49   this->_AddInput( "Input" );
50
51   this->m_Parameters->ConfigureAsString( "FileName", "" );
52 }
53
54 // -------------------------------------------------------------------------
55 cpPlugins::IO::ImageWriter::
56 ~ImageWriter( )
57 {
58 }
59
60 // -------------------------------------------------------------------------
61 std::string cpPlugins::IO::ImageWriter::
62 _GenerateData( )
63 {
64   // Thank you very much Microsoft !!! :-@
65   std::string   r = this->_GD0_Image< 2 >( );
66   if( r != "" ) r = this->_GD0_Image< 3 >( );
67   if( r != "" ) r = this->_GD0_Image< 4 >( );
68   if( r != "" ) r = this->_GD0_VectorImage< 2 >( );
69   if( r != "" ) r = this->_GD0_VectorImage< 3 >( );
70   if( r != "" ) r = this->_GD0_VectorImage< 4 >( );
71   return( r );
72 }
73
74 // -------------------------------------------------------------------------
75 template< unsigned int D >
76 std::string cpPlugins::IO::ImageWriter::
77 _GD0_Image( )
78 {
79   cpPlugins::Interface::Image* image =
80     this->GetInput< cpPlugins::Interface::Image >( "Input" );
81   if( image == NULL )
82     return( "ImageWriter: No input image." );
83
84   itk::DataObject* itk_image = NULL;
85   std::string r = "";
86   cpPlugins_Image_Demangle_AllTypes( D, image, itk_image, r, _RealGD );
87   else r = "ImageWriter: Input image type not supported.";
88
89   return( r );
90 }
91
92 // -------------------------------------------------------------------------
93 template< unsigned int D >
94 std::string cpPlugins::IO::ImageWriter::
95 _GD0_VectorImage( )
96 {
97   cpPlugins::Interface::Image* image =
98     this->GetInput< cpPlugins::Interface::Image >( "Input" );
99   if( image == NULL )
100     return( "ImageWriter: No input image." );
101
102   itk::DataObject* itk_image = NULL;
103   std::string r = "";
104   cpPlugins_VectorImage_Demangle_AllTypes( D, image, itk_image, r, _RealGD );
105   else r = "ImageWriter: Input image type not supported.";
106
107   return( r );
108 }
109
110 // -------------------------------------------------------------------------
111 template< class I >
112 std::string cpPlugins::IO::ImageWriter::
113 _RealGD( itk::DataObject* image )
114 {
115   // Get filename
116   std::string fname = this->m_Parameters->GetString( "FileName" );
117
118   typedef itk::ImageFileWriter< I > _W;
119   _W* writer = this->_CreateITK< _W >( );
120   writer->SetFileName( fname );
121   writer->SetInput( dynamic_cast< I* >( image ) );
122   try
123   {
124     writer->Update( );
125   }
126   catch( itk::ExceptionObject& err )
127   {
128     return( "ImageWriter: " + std::string( err.GetDescription( ) ) );
129
130   } // yrt
131   return( "" );
132 }
133
134 // eof - $RCSfile$