]> Creatis software - bbtk.git/blob - packages/itkvtk/src/bbitkvtkvtkImageData2itkImage.cxx
Fix mistyping
[bbtk.git] / packages / itkvtk / src / bbitkvtkvtkImageData2itkImage.cxx
1 /*=========================================================================
2                                                                                
3   Program:   bbtk
4   Module:    $RCSfile: bbitkvtkvtkImageData2itkImage.cxx,v $
5   Language:  C++
6   Date:      $Date: 2011/06/16 13:38:49 $
7   Version:   $Revision: 1.7 $
8
9 =========================================================================*/
10
11 /* ---------------------------------------------------------------------
12
13 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
14 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
15 *
16 *  This software is governed by the CeCILL-B license under French law and 
17 *  abiding by the rules of distribution of free software. You can  use, 
18 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
19 *  license as circulated by CEA, CNRS and INRIA at the following URL 
20 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
21 *  or in the file LICENSE.txt.
22 *
23 *  As a counterpart to the access to the source code and  rights to copy,
24 *  modify and redistribute granted by the license, users are provided only
25 *  with a limited warranty  and the software's author,  the holder of the
26 *  economic rights,  and the successive licensors  have only  limited
27 *  liability.
28 *
29 *  The fact that you are presently reading this means that you have had
30 *  knowledge of the CeCILL-B license and that you accept its terms.
31 * ------------------------------------------------------------------------ */
32
33 /**
34  *  \file 
35  *  \brief 
36  */
37 #ifdef _USE_ITK_
38 #ifdef _USE_VTK_
39
40 #include "bbitkvtkvtkImageData2itkImage.h"
41 #include "bbitkvtkPackage.h"
42 #include "itkVTKImageToImageFilter.h"
43
44
45 namespace bbitkvtk 
46 {
47
48   BBTK_BLACK_BOX_IMPLEMENTATION(vtkImageData2itkImage,bbtk::AtomicBlackBox);
49
50   BBTK_ADD_BLACK_BOX_TO_PACKAGE(itkvtk,vtkImageData2itkImage);
51
52 #define BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH_DIM(I,DIM,M)                        \
53   if (I->GetDataDimension()==DIM)                                               \
54     {                                                                           \
55       if (I->GetScalarType()==VTK_CHAR) M<char,DIM>();                          \
56       else if (I->GetScalarType()==VTK_SIGNED_CHAR) M<signed char,DIM>();       \
57       else if (I->GetScalarType()==VTK_UNSIGNED_CHAR) M<unsigned char,DIM>();   \
58       else if (I->GetScalarType()==VTK_SHORT) M<short,DIM>();                   \
59       else if (I->GetScalarType()==VTK_UNSIGNED_SHORT) M<unsigned short,DIM>(); \
60       else if (I->GetScalarType()==VTK_INT) M<int,DIM>();                       \
61       else if (I->GetScalarType()==VTK_UNSIGNED_INT) M<unsigned int,DIM>();     \
62       else if (I->GetScalarType()==VTK_LONG) M<long,DIM>();                     \
63       else if (I->GetScalarType()==VTK_UNSIGNED_LONG) M<unsigned long,DIM>();   \
64       else if (I->GetScalarType()==VTK_FLOAT) M<float,DIM>();                   \
65       else if (I->GetScalarType()==VTK_DOUBLE) M<double,DIM>();                 \
66     }
67
68 #define BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH(I,M) \
69   BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH_DIM(I,2,M) \
70   BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH_DIM(I,3,M)
71
72   void vtkImageData2itkImage::Convert()
73   {
74      BBTK_TEMPLATE_VTK_IMAGE_DATA_SWITCH(bbGetInputIn(),Convert)
75   }
76
77
78    template<class T, unsigned int D>
79    void vtkImageData2itkImage::Convert()
80    {
81     bbtkDebugMessage("process",5,"==> ["<<bbGetFullName()<<"] : Convert<"
82                         <<bbtk::TypeName<T>()<<","<<D
83                         <<">()"<<std::endl);
84
85
86     typedef itk::Image<T,D> itkImageType;
87     typedef itk::VTKImageToImageFilter< itkImageType > VtkToItkConnection;
88     typename VtkToItkConnection::Pointer conv;
89
90     // No converter yet : create it and set its input
91     if (!mConverter) 
92       {
93         bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : No converter yet : creating it"<<std::endl);
94         conv = VtkToItkConnection::New();
95         mConverter = conv;
96         conv->SetInput( this->bbGetInputIn() );
97       }
98     else 
99       {
100         bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : Converter ok "<<std::endl);
101         // Input vtkImage type changed ? 
102         // Have to change the converter and set its input
103         conv = dynamic_cast<VtkToItkConnection*>
104                            ((itk::ProcessObject*)mConverter);
105         if (!conv) 
106           {
107             bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : Converter of wrong input type : reacreating it "<<std::endl);
108
109             mConverter->UnRegister();
110             conv = VtkToItkConnection::New();
111             mConverter = conv;
112             conv->SetInput( this->bbGetInputIn() );
113           }
114         // Input image type did not change but input image pointer did:
115         // set new input
116         else if ( this->bbGetInputIn() != conv->GetExporter()->GetInput())
117           {
118             bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : Converter input changed : resetting it"<<std::endl);
119
120             conv->SetInput( this->bbGetInputIn() );
121           }
122         else 
123           {
124             bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : Nothing changed"<<std::endl);     
125           }
126       }
127     try
128       {
129         
130         bbtkDebugMessage("process",5,"    ["<<bbGetFullName()<<"] : Trying update"<<std::endl);  
131         conv->Update();
132         itkImageType* out = const_cast<itkImageType*>(conv->GetOutput());
133         bbSetOutputOut(out);
134       }
135     catch( itk::ExceptionObject & e) 
136       {
137         bbtkError("vtkImageData2itkImage<"
138                   <<bbtk::TypeName<T>()<<","<<D
139                   <<">::Convert() : "<<e);
140       } 
141     bbtkDebugMessage("process",5,"<== ["<<bbGetFullName()<<"] : Convert<"
142                      <<bbtk::TypeName<T>()<<","<<D
143                      <<">() *DONE*"<<std::endl);
144 }
145
146
147   void vtkImageData2itkImage::bbUserSetDefaultValues()
148   {
149     //    bbSetOutputOut(NULL);
150     mConverter = 0;
151   }
152
153   void vtkImageData2itkImage::bbUserInitializeProcessing()
154   {
155   }
156
157   void vtkImageData2itkImage::bbUserFinalizeProcessing()
158   {
159   }
160
161
162 }
163 // eo namespace bbtk
164
165 #endif
166 // _USE_VTK_
167 #endif
168 // _USE_ITK_