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