]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Plugins/IO/ImageWriter.cxx
Kind of bored: graph editor debugged
[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   this->m_Parameters->ConfigureAsString( "FileName" );
51 }
52
53 // -------------------------------------------------------------------------
54 cpPlugins::IO::ImageWriter::
55 ~ImageWriter( )
56 {
57 }
58
59 // -------------------------------------------------------------------------
60 std::string cpPlugins::IO::ImageWriter::
61 _GenerateData( )
62 {
63   // Thank you very much Microsoft !!! :-@
64   std::string   r = this->_GD0_Image< 2 >( );
65   if( r != "" ) r = this->_GD0_Image< 3 >( );
66   if( r != "" ) r = this->_GD0_Image< 4 >( );
67   if( r != "" ) r = this->_GD0_VectorImage< 2 >( );
68   if( r != "" ) r = this->_GD0_VectorImage< 3 >( );
69   if( r != "" ) r = this->_GD0_VectorImage< 4 >( );
70   return( r );
71 }
72
73 // -------------------------------------------------------------------------
74 template< unsigned int D >
75 std::string cpPlugins::IO::ImageWriter::
76 _GD0_Image( )
77 {
78   cpPlugins::Interface::Image* image =
79     this->GetInput< cpPlugins::Interface::Image >( "Input" );
80   if( image == NULL )
81     return( "ImageWriter: No input image." );
82
83   itk::DataObject* itk_image = NULL;
84   std::string r = "";
85   cpPlugins_Image_Demangle_AllTypes( D, image, itk_image, r, _RealGD );
86   else r = "ImageWriter: Input image type not supported.";
87
88   return( r );
89 }
90
91 // -------------------------------------------------------------------------
92 template< unsigned int D >
93 std::string cpPlugins::IO::ImageWriter::
94 _GD0_VectorImage( )
95 {
96   cpPlugins::Interface::Image* image =
97     this->GetInput< 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->GetString( "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$