]> Creatis software - bbtk.git/blob - packages/itk/src/bbitkExtractImageFilter.h
Feature #1774
[bbtk.git] / packages / itk / src / bbitkExtractImageFilter.h
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 /*=========================================================================
29   Program:   bbtk
30   Module:    $RCSfile: bbitkExtractImageFilter.h,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:50:39 $
33   Version:   $Revision: 1.11 $
34 =========================================================================*/
35                                                                      
36
37
38 #ifdef _USE_ITK_
39
40 #include "bbtkAtomicBlackBox.h"
41 #include "itkExtractImageFilter.h"
42 #include "bbtkItkBlackBoxMacros.h"
43 #include "bbitkImage.h"
44 #include "bbitkImageRegion.h"
45
46 namespace bbitk
47 {
48
49   //===================================================
50   /// Template filter
51   template <class T>
52   class /*BBTK_EXPORT*/ ExtractImageFilter
53     : 
54     public bbtk::AtomicBlackBox,
55     public itk::ExtractImageFilter<T,T>
56   {
57     BBTK_TEMPLATE_BLACK_BOX_INTERFACE(ExtractImageFilter,
58                                       bbtk::AtomicBlackBox,
59                                       T);
60     typedef itk::ExtractImageFilter<T,T> itkParent;
61
62     BBTK_DECLARE_ITK_INPUT(In,const T*);
63     BBTK_DECLARE_ITK_PARAM(ExtractionRegion,typename T::RegionType);
64     BBTK_DECLARE_ITK_OUTPUT(Out,T*);
65
66     BBTK_ITK_PROCESS();
67     BBTK_ITK_DELETE();
68   };
69   //===================================================
70
71
72   //===================================================
73   // Descriptor of template filter 
74   BBTK_BEGIN_DESCRIBE_TEMPLATE_BLACK_BOX(ExtractImageFilter,
75                                       bbtk::AtomicBlackBox);
76   BBTK_NAME("ExtractImageFilter<"+bbtk::TypeName<T>()+">");
77   BBTK_AUTHOR("laurent.guigues@creatis.insa-lyon.fr");
78   BBTK_DESCRIPTION("Decrease the image size by cropping the image to the selected region bounds (bbification of itk::ExtractImageFilter)");
79   BBTK_TEMPLATE_INPUT(ExtractImageFilter,In,"Input image",const T*);
80   BBTK_TEMPLATE_INPUT(ExtractImageFilter,ExtractionRegion,"Extraction region",typename T::RegionType);
81   BBTK_TEMPLATE_OUTPUT(ExtractImageFilter,Out,"Output image",T*);
82   BBTK_END_DESCRIBE_TEMPLATE_BLACK_BOX(ExtractImageFilter);
83   //===================================================
84
85   //===================================================
86   template <class T> void ExtractImageFilter<T>::bbUserSetDefaultValues() {}
87   template <class T> void ExtractImageFilter<T>::bbUserInitializeProcessing(){}
88   template <class T> void ExtractImageFilter<T>::bbUserFinalizeProcessing() {}
89   //===================================================
90   
91
92
93
94
95
96
97
98
99   //===================================================
100   // Generic "untemplatized" filter
101   //===================================================
102   class /*BBTK_EXPORT*/ ExtractImageFilterGeneric
103     : public bbtk::AtomicBlackBox
104   {
105     BBTK_BLACK_BOX_INTERFACE(ExtractImageFilterGeneric,
106                              bbtk::AtomicBlackBox);
107     BBTK_DECLARE_INPUT(In,anyImagePointer);
108     BBTK_DECLARE_INPUT(Region,anyImageRegion);
109     BBTK_DECLARE_OUTPUT(Out,anyImagePointer);
110     BBTK_PROCESS(ProcessSwitch);
111   private :
112     void ProcessSwitch();
113     template <class T> void Process();
114   };
115   //===================================================
116   
117   //===================================================
118   // Descriptor of generic filter
119   BBTK_BEGIN_DESCRIBE_BLACK_BOX(ExtractImageFilterGeneric,
120                                 bbtk::AtomicBlackBox);
121   BBTK_NAME("ExtractImageFilter");
122   BBTK_AUTHOR("laurent.guigues at creatis.insa-lyon.fr");
123   BBTK_CATEGORY("image;filter");
124   BBTK_DESCRIPTION("Decrease the image size by cropping the image to the selected region bounds (bbification of itk::ExtractImageFilter)");
125   BBTK_INPUT(ExtractImageFilterGeneric,In,"Input image",anyImagePointer,"");
126   BBTK_INPUT(ExtractImageFilterGeneric,Region,"Extraction region",anyImageRegion,"");
127   BBTK_OUTPUT(ExtractImageFilterGeneric,Out,"Output image",anyImagePointer,"");
128   BBTK_END_DESCRIBE_BLACK_BOX(ExtractImageFilterGeneric);
129   //===================================================
130
131
132   //===================================================
133   // The main processing method is template on the itk image type 
134   // which was set to the input. 
135   // It is called by the ProcessSwitch method which checks the input In type
136   // and calls Process<T> where T is the right type.
137   template <class T>
138   void ExtractImageFilterGeneric::Process()
139   {
140     bbtkDebugMessageInc("Core",9,
141                         "bbitk::ExtractImageFilterGeneric::Process<"
142                         <<bbtk::TypeName<T>()<<">()"<<std::endl);
143     // T is the type of input image
144     typedef T ImageType;
145     // We define the type of the filter to use which is template 
146     // on the image type
147     typedef ExtractImageFilter<ImageType> FilterType;
148     // Allocate the filter
149     typename FilterType::Pointer f = FilterType::New("Temp");
150
151     // Set the input In of the filter
152     // Here we :
153     // 1) Get **OUR** input 'In' by bbGetInputIn()
154     //    It is a anyImagePointer
155     // 2) We **KNOW** that the actual type stored by the anyImagePointer
156     //    is ImageType* hence we get the value stored using the 
157     //    unsafe_get method (which is a template method on the return type)
158     f->bbSetInputIn( this->bbGetInputIn().unsafe_get<ImageType*>() );
159     
160     // The same for the ImageRegion except that we use 'get' and not 
161     // 'unsafe get' because the region dimension passed by the user may not be 
162     // compatible with the image dimension.
163     // If get fails it throws an exception which must be caught outside 
164     f->bbSetInputExtractionRegion ( this->bbGetInputRegion().get<typename T::RegionType>() );
165
166     // Execute the filter
167     f->bbExecute();
168     // Register its output or it will be deleted we delete the filter
169     f->bbGetOutputOut()->Register();
170     // Set OUR output
171     this->bbSetOutputOut( f->bbGetOutputOut() );
172     // Delete the temporary filter
173     f->bbDelete();
174
175     bbtkDebugDecTab("Core",9);
176   }
177   //===================================================
178
179 }
180 // EO namespace bbitk
181
182 #endif