]> Creatis software - cpPlugins.git/blob - plugins/ITKImageGenericFilters_1/RegionOfInterestImageFilter.cxx
9254bf4d83aa5a0c7f461938bf106b0e88aa71dd
[cpPlugins.git] / plugins / ITKImageGenericFilters_1 / RegionOfInterestImageFilter.cxx
1 #include <ITKImageGenericFilters_1/RegionOfInterestImageFilter.h>
2 #include <cpInstances/Image.h>
3 #include <cpInstances/BoundingBox.h>
4 #include <cpInstances/Image_Demanglers.h>
5
6 #include <itkRGBPixel.h>
7 #include <itkRGBAPixel.h>
8 #include <itkRegionOfInterestImageFilter.h>
9
10 // -------------------------------------------------------------------------
11 cpPluginsITKImageGenericFilters_1::RegionOfInterestImageFilter::
12 RegionOfInterestImageFilter( )
13   : Superclass( )
14 {
15   typedef cpInstances::Image _TImage;
16   typedef cpInstances::BoundingBox _TBBox;
17   this->_ConfigureInput< _TImage >( "Input", true, false );
18   this->_ConfigureInput< _TBBox >( "BoundingBox", true, false );
19   this->_ConfigureOutput< _TImage >( "Output" );
20 }
21
22 // -------------------------------------------------------------------------
23 cpPluginsITKImageGenericFilters_1::RegionOfInterestImageFilter::
24 ~RegionOfInterestImageFilter( )
25 {
26 }
27
28 // -------------------------------------------------------------------------
29 void cpPluginsITKImageGenericFilters_1::RegionOfInterestImageFilter::
30 _GenerateData( )
31 {
32   auto o = this->GetInputData( "Input" );
33   cpPlugins_Demangle_Image_ScalarPixels_AllDims_1( o, _GD0 )
34     this->_Error( "Invalid input image." );
35 }
36
37 // -------------------------------------------------------------------------
38 template< class _TImage >
39 void cpPluginsITKImageGenericFilters_1::RegionOfInterestImageFilter::
40 _GD0( _TImage* input )
41 {
42   typedef cpInstances::BoundingBox _TBBox;
43   typedef itk::RegionOfInterestImageFilter< _TImage, _TImage > _TFilter;
44   typedef typename _TImage::IndexType  _TIndex;
45   typedef typename _TImage::PointType  _TPoint;
46   typedef typename _TImage::RegionType _TRegion;
47   typedef typename _TImage::SizeType   _TSize;
48
49   auto bb = this->GetInput< _TBBox >( "BoundingBox" );
50
51   _TIndex i0, i1;
52   input->TransformPhysicalPointToIndex( bb->GetMinimum< _TPoint >( ), i0 );
53   input->TransformPhysicalPointToIndex( bb->GetMaximum< _TPoint >( ), i1 );
54
55   _TSize size;
56   for( unsigned int d = 0; d < _TImage::ImageDimension; ++d )
57     size[ d ] = i1[ d ] - i0[ d ] + 1;
58
59   _TRegion region;
60   region.SetIndex( i0 );
61   region.SetSize( size );
62
63   auto filter = this->_CreateITK< _TFilter >( );
64   filter->SetInput( input );
65   filter->SetRegionOfInterest( region );
66   filter->Update( );
67   this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) );
68 }
69
70 // eof - $RCSfile$