]> Creatis software - cpPlugins.git/blob - plugins/ITKBinaryFunctorFilters/BinaryArithmeticImageFilter.cxx
Cast image filter added. ROI filter modified.
[cpPlugins.git] / plugins / ITKBinaryFunctorFilters / BinaryArithmeticImageFilter.cxx
1 #include <ITKBinaryFunctorFilters/BinaryArithmeticImageFilter.h>
2 #include <cpInstances/Image.h>
3
4 #include <itkAddImageFilter.h>
5 #include <itkSubtractImageFilter.h>
6 #include <itkMultiplyImageFilter.h>
7 #include <itkDivideImageFilter.h>
8 #include <itkDivideOrZeroOutImageFilter.h>
9 #include <itkPowImageFilter.h>
10
11 // -------------------------------------------------------------------------
12 cpPluginsITKBinaryFunctorFilters::BinaryArithmeticImageFilter::
13 BinaryArithmeticImageFilter( )
14   : Superclass( )
15 {
16   typedef cpInstances::Image _TImage;
17
18   this->_ConfigureInput< _TImage >( "Input1", true, false );
19   this->_ConfigureInput< _TImage >( "Input2", true, false );
20   this->_ConfigureOutput< _TImage >( "Output" );
21
22   std::vector< std::string > choices;
23   choices.push_back( "Add" );
24   choices.push_back( "Subtract" );
25   choices.push_back( "Multiply" );
26   choices.push_back( "Divide" );
27   choices.push_back( "DivideOrZero" );
28   choices.push_back( "Pow" );
29   this->m_Parameters.ConfigureAsChoices( "Operator", choices );
30 }
31
32 // -------------------------------------------------------------------------
33 cpPluginsITKBinaryFunctorFilters::BinaryArithmeticImageFilter::
34 ~BinaryArithmeticImageFilter( )
35 {
36 }
37
38 // -------------------------------------------------------------------------
39 void cpPluginsITKBinaryFunctorFilters::BinaryArithmeticImageFilter::
40 _GenerateData( )
41 {
42   auto o = this->GetInputData( "Input1" );
43   cpPlugins_Demangle_Image_IntPixels_AllDims_1( o, _GD0 )
44     this->_Error( "Invalid input image." );
45 }
46
47 // -------------------------------------------------------------------------
48 template< class _TImage >
49 void cpPluginsITKBinaryFunctorFilters::BinaryArithmeticImageFilter::
50 _GD0( _TImage* image1 )
51 {
52   typedef itk::AddImageFilter< _TImage, _TImage > _TAdd;
53   typedef itk::SubtractImageFilter< _TImage, _TImage > _TSubtract;
54   typedef itk::MultiplyImageFilter< _TImage, _TImage > _TMultiply;
55   typedef itk::DivideImageFilter< _TImage, _TImage, _TImage > _TDivide;
56   typedef itk::DivideOrZeroOutImageFilter< _TImage, _TImage > _TDivideOrZero;
57   typedef itk::PowImageFilter< _TImage, _TImage > _TPow;
58
59   auto image2 = this->GetInputData< _TImage >( "Input2" );
60   if( image2 == NULL )
61     this->_Error( "Incompatible second input image." );
62   std::string op = this->m_Parameters.GetSelectedChoice( "Operator" );
63   if( op == "Add" )
64     this->_GD1< _TAdd, _TImage >( image1, image2 );
65   else if( op == "Subtract" )
66     this->_GD1< _TSubtract, _TImage >( image1, image2 );
67   else if( op == "Multiply" )
68     this->_GD1< _TMultiply, _TImage >( image1, image2 );
69   else if( op == "Divide" )
70     this->_GD1< _TDivide, _TImage >( image1, image2 );
71   else if( op == "DivideOrZero" )
72     this->_GD1< _TDivideOrZero, _TImage >( image1, image2 );
73   else if( op == "Pow" )
74     this->_GD1< _TPow, _TImage >( image1, image2 );
75 }
76
77 // -------------------------------------------------------------------------
78 template< class _TFilter, class _TImage >
79 void cpPluginsITKBinaryFunctorFilters::BinaryArithmeticImageFilter::
80 _GD1( _TImage* image1, _TImage* image2 )
81 {
82   // Configure filter
83   auto filter = this->_CreateITK< _TFilter >( );
84   filter->SetInput1( image1 );
85   filter->SetInput2( image2 );
86   filter->Update( );
87
88   // Connect output
89   this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) );
90 }
91
92 // eof - $RCSfile$