]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkCreateImage.cxx
#3464 Bug CreateImage box in 2D (spacing)
[bbtk.git] / packages / vtk / src / bbvtkCreateImage.cxx
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 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
30 //===== 
31 #include "bbvtkCreateImage.h"
32 #include "bbvtkPackage.h"
33 namespace bbvtk
34 {
35
36 BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,CreateImage)
37 BBTK_BLACK_BOX_IMPLEMENTATION(CreateImage,bbtk::AtomicBlackBox);
38 //===== 
39 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
40 //===== 
41 void CreateImage::Process()
42 {
43
44 // THE MAIN PROCESSING METHOD BODY
45 //   Here we simply set the input 'In' value to the output 'Out'
46 //   And print out the output value
47 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
48 //    void bbSet{Input|Output}NAME(const TYPE&)
49 //    const TYPE& bbGet{Input|Output}NAME() const 
50 //    Where :
51 //    * NAME is the name of the input/output
52 //      (the one provided in the attribute 'name' of the tag 'input')
53 //    * TYPE is the C++ type of the input/output
54 //      (the one provided in the attribute 'type' of the tag 'input')
55
56
57         int             dim[3]; 
58         dim[0]  = bbGetInputDimensions()[0];
59         dim[1]  = bbGetInputDimensions()[1];
60         dim[2]  = bbGetInputDimensions()[2];
61
62
63
64         if (imageoutput!=NULL) 
65         {
66                 imageoutput->Delete();
67                 imageoutput = NULL;
68         } // if imageoutput
69
70         if ( !((dim[0]<=0)||(dim[1]<=0)||(dim[2]<=0)) )
71         {
72                 double  spc[3];
73                 int             outputformat = VTK_UNSIGNED_SHORT;
74
75                 spc[0]  = 1;
76                 spc[1]  = 1;
77                 spc[2]  = 1;
78                 if  ( bbGetInputSpacing().size()==3 )
79                 {
80                         spc[0] = bbGetInputSpacing()[0];
81                         spc[1] = bbGetInputSpacing()[1];
82                         spc[2] = bbGetInputSpacing()[2];
83                         if ((spc[0]<=0)||(spc[1]<=0)||(spc[2]<=0))
84                         {
85                                 spc[0] = 1;
86                                 spc[1] = 1;
87                                 spc[2] = 1;
88                         }
89                 } // size spc 3
90                 if  ( bbGetInputSpacing().size()==2 )
91                 {
92                         spc[0] = bbGetInputSpacing()[0];
93                         spc[1] = bbGetInputSpacing()[1];
94                         if ((spc[0]<=0)||(spc[1]<=0))
95                         {
96                                 spc[0] = 1;
97                                 spc[1] = 1;
98                         }
99                 } // if size spc 2
100
101                 
102                 if (bbGetInputOutputFormat()=="VTK_BIT")                                        outputformat = VTK_BIT;                 // 1
103                 else if (bbGetInputOutputFormat()=="VTK_CHAR")                          outputformat = VTK_CHAR;                                // 2
104                 else if (bbGetInputOutputFormat()=="MET_CHAR")                          outputformat = VTK_CHAR;                                // 2
105                 else if (bbGetInputOutputFormat()=="VTK_SIGNED_CHAR")           outputformat = VTK_SIGNED_CHAR;         // 15
106                 else if (bbGetInputOutputFormat()=="MET_UCHAR")                         outputformat = VTK_UNSIGNED_CHAR;       // 3
107                 else if (bbGetInputOutputFormat()=="VTK_UNSIGNED_CHAR")         outputformat = VTK_UNSIGNED_CHAR;       // 3
108                 else if (bbGetInputOutputFormat()=="VTK_SHORT")                         outputformat = VTK_SHORT;               // 4
109                 else if (bbGetInputOutputFormat()=="MET_SHORT")                         outputformat = VTK_SHORT;               // 4
110                 else if (bbGetInputOutputFormat()=="VTK_UNSIGNED_SHORT")        outputformat = VTK_UNSIGNED_SHORT;      // 5
111                 else if (bbGetInputOutputFormat()=="MET_USHORT")                        outputformat = VTK_UNSIGNED_SHORT;      // 5
112                 else if (bbGetInputOutputFormat()=="VTK_INT")                           outputformat = VTK_INT;                 // 6
113                 else if (bbGetInputOutputFormat()=="MET_INT")                           outputformat = VTK_INT;                 // 6
114                 else if (bbGetInputOutputFormat()=="VTK_UNSIGNED_INT")          outputformat = VTK_UNSIGNED_INT;        // 7 
115                 else if (bbGetInputOutputFormat()=="MEY_UINT")                          outputformat = VTK_UNSIGNED_INT;        // 7 
116                 else if (bbGetInputOutputFormat()=="VTK_LONG")                          outputformat = VTK_LONG;                // 8
117                 else if (bbGetInputOutputFormat()=="MET_LONG")                          outputformat = VTK_LONG;                // 8
118                 else if (bbGetInputOutputFormat()=="VTK_UNSIGNED_LONG")         outputformat = VTK_UNSIGNED_LONG;       // 9
119                 else if (bbGetInputOutputFormat()=="MET_ULONG")                         outputformat = VTK_UNSIGNED_LONG;       // 9
120                 else if (bbGetInputOutputFormat()=="VTK_FLOAT")                         outputformat = VTK_FLOAT;               // 10
121                 else if (bbGetInputOutputFormat()=="MET_FLOAT")                         outputformat = VTK_FLOAT;               // 10
122                 else if (bbGetInputOutputFormat()=="VTK_DOUBLE")                        outputformat = VTK_DOUBLE;              // 11           
123                 else if (bbGetInputOutputFormat()=="MET_DOUBLE")                        outputformat = VTK_DOUBLE;              // 11  
124
125                 if ((dim[0]<=0)||(dim[1]<=0)||(dim[2]<=0))
126                 {
127                         dim[0] = 1;
128                         dim[1] = 1;
129                         dim[2] = 1;
130                 }
131
132                 imageoutput = vtkImageData::New();
133                 imageoutput->Initialize();
134                 imageoutput->SetSpacing( spc );
135                 imageoutput->SetDimensions(  dim[0], dim[1], dim[2] );
136
137         //EED 2017-01-01 Migration VTK7
138         #if (VTK_MAJOR_VERSION <= 5) 
139                 imageoutput->SetScalarType( outputformat );     
140                 imageoutput->AllocateScalars();
141         #endif
142         #if (VTK_MAJOR_VERSION >= 6) 
143                 imageoutput->AllocateScalars( outputformat,1 );
144         #endif
145                 
146                 int i,j,k;
147                 long sizeBlock; 
148                 for (i=0; i<dim[0]; i++)
149                 {
150                         imageoutput->SetScalarComponentFromDouble(i,0,0, 0, bbGetInputInitialValue() );
151                 } // for i
152                 sizeBlock= dim[0] * imageoutput->GetScalarSize();
153                 for (j=1; j<dim[1]; j++)
154                 {
155                         memcpy(  imageoutput->GetScalarPointer(0,j,0), imageoutput->GetScalarPointer()  , sizeBlock );
156                 } // for j              
157                 sizeBlock= dim[0] * dim[1] * imageoutput->GetScalarSize();
158                 for (k=0; k<dim[2]; k++)
159                 {
160                         memcpy(  imageoutput->GetScalarPointer(0,0,k) ,imageoutput->GetScalarPointer()  , sizeBlock );
161                 } // for k
162         //EED 2017-01-01 Migration VTK7
163         #if (VTK_MAJOR_VERSION <= 5) 
164                 imageoutput->Update();
165         #endif
166         #if (VTK_MAJOR_VERSION >= 6) 
167                 // ..
168         #endif
169                 imageoutput->Modified();
170         } // if dim = 0
171
172         bbSetOutputOut(imageoutput);
173         
174 }       
175         
176 //===== 
177 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
178 //===== 
179 void CreateImage::bbUserSetDefaultValues()
180 {
181         imageoutput=NULL;
182
183 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
184         std::vector<int> dim;
185         dim.push_back(250);
186         dim.push_back(250);
187         dim.push_back(250);
188         std::vector<double> spc;
189         spc.push_back(1.0);
190         spc.push_back(1.0);
191         spc.push_back(1.0);
192         
193         bbSetInputDimensions(dim);
194         bbSetInputSpacing(spc);
195         bbSetInputOutputFormat("VTK_UNSIGNED_SHORT");
196         bbSetInputInitialValue(0);
197 }
198         
199 //===== 
200 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
201 //===== 
202 void CreateImage::bbUserInitializeProcessing()
203 {
204
205 //  THE INITIALIZATION METHOD BODY :
206 //    Here does nothing 
207 //    but this is where you should allocate the internal/output pointers 
208 //    if any 
209
210 //EED 2017-12-7         imageoutput = vtkImageData::New();
211
212 }
213         
214 //===== 
215 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
216 //===== 
217 void CreateImage::bbUserFinalizeProcessing()
218 {
219
220 //  THE FINALIZATION METHOD BODY :
221 //    Here does nothing 
222 //    but this is where you should desallocate the internal/output pointers 
223 //    if any
224         imageoutput->Delete();
225 }
226
227 }// EO namespace bbvtk
228
229