]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkMetaImageWriter.cxx
MetaImageWriter Block
[bbtk.git] / packages / vtk / src / bbvtkMetaImageWriter.cxx
1 //===== 
2 // 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)
3 //===== 
4 #include "bbvtkMetaImageWriter.h"
5 #include "bbvtkPackage.h"
6
7 #include <vtkExtractVOI.h> 
8 #include <vtkMetaImageWriter.h> 
9
10 namespace bbvtk
11 {
12
13 BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,MetaImageWriter)
14 BBTK_BLACK_BOX_IMPLEMENTATION(MetaImageWriter,bbtk::AtomicBlackBox);
15
16
17
18 void MetaImageWriter::Save_mhdb( std::string filename  , vtkImageData* img ,int sizeB)
19 {
20         int i,j,k;
21         int ext[6];
22         int dim[3];
23         img->GetExtent(ext);
24         dim[0]=ext[1]-ext[0]+1;
25         dim[1]=ext[3]-ext[2]+1;
26         dim[2]=ext[5]-ext[4]+1;
27         
28         int bsx=ceil((double)dim[0]/(double)sizeB);
29         int bsy=ceil((double)dim[1]/(double)sizeB);
30         int bsz=ceil((double)dim[2]/(double)sizeB);
31         
32         printf("EED MetaImageWriter::Save_mhdb  bsx bsy bsz  = %d %d %d\n", bsx,bsy,bsz);       
33         printf("EED MetaImageWriter::Save_mhdb  dim          = %d %d %d\n", dim[0],dim[1],dim[2]  );
34         
35         int voi[6];
36         std::string filenameBlock;
37         
38         for (i=0;i<bsx;i++)
39         {
40                 for (j=0;j<bsy;j++)
41                 {
42                         for (k=0;k<bsz;k++)
43                         {
44                                 voi[0]=i*sizeB;
45                                 voi[1]=voi[0]+(sizeB-1);
46                                 voi[2]=j*sizeB;
47                                 voi[3]=voi[2]+(sizeB-1);
48                                 voi[4]=k*sizeB;
49                                 voi[5]=voi[4]+(sizeB-1);
50
51 /*
52                                 voi[0]=0;
53                                 voi[1]=10;
54                                 voi[2]=0;
55                                 voi[3]=10;
56                                 voi[4]=0;
57                                 voi[5]=10;
58         */              
59                                 if (voi[1]>=dim[0]) { voi[1]=dim[0]-1; } 
60                                 if (voi[3]>=dim[1]) { voi[3]=dim[1]-1; } 
61                                 if (voi[5]>=dim[2]) { voi[5]=dim[2]-1; } 
62                                 
63                                 vtkExtractVOI *extract = vtkExtractVOI::New();
64                                 extract->SetInputData( img );
65                                 extract->SetVOI(voi);   
66                                 extract->UpdateWholeExtent();
67                                 extract->Modified();
68                                 extract->Update();
69
70                                 vtkMetaImageWriter* w = vtkMetaImageWriter::New();
71                                 w->SetInputData( extract->GetOutput() );
72                                 w->SetCompression(true);  
73                                 w->SetFileDimensionality(bbGetInputIn()->GetDataDimension());   // NTU
74                                 filenameBlock=filename+"/a-"+ std::to_string(i)+"-"+ std::to_string(j)+"-"+ std::to_string(k)+".mhd";
75 printf("EED MetaImageWriter::Save_mhdb  name  = %s \n", filenameBlock.c_str() );        
76                                 w->SetFileName( filenameBlock.c_str() );  
77 //                              w->Write();
78                                 
79                                 w->Delete();
80                                 extract->Delete();
81                         } // for k
82                 } // for j
83         } // for i
84         
85                 
86 }
87
88
89
90 //===== 
91 // 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)
92 //===== 
93 void MetaImageWriter::Process()
94 {
95
96 // THE MAIN PROCESSING METHOD BODY
97 //   Here we simply set the input 'In' value to the output 'Out'
98 //   And print out the output value
99 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
100 //    void bbSet{Input|Output}NAME(const TYPE&)
101 //    const TYPE& bbGet{Input|Output}NAME() const 
102 //    Where :
103 //    * NAME is the name of the input/output
104 //      (the one provided in the attribute 'name' of the tag 'input')
105 //    * TYPE is the C++ type of the input/output
106 //      (the one provided in the attribute 'type' of the tag 'input')
107
108
109 //EED 2017-01-01 Migration VTK7
110 #if VTK_MAJOR_VERSION <= 5
111    bbGetInputIn()->Update();
112 #else
113         // ...
114 #endif
115
116         std::string userGivenName = bbGetInputFilename();
117         if (bbGetInputIn()!=NULL)
118         {
119                 if (userGivenName.substr(userGivenName.size()-5) == ".mhdb")  
120                 { 
121                         printf("EED vtkMetaImageWriter process  block..ups\n");
122                         
123                         Save_mhdb( userGivenName , bbGetInputIn() , 20 );
124                         
125                 } else {
126                                 if (userGivenName.substr(userGivenName.size()-4) != ".mhd")  //JPR
127                                 { 
128                                         userGivenName += ".mhd";
129                                 }
130                            vtkMetaImageWriter* w = vtkMetaImageWriter::New();
131         //EED 2017-01-01 Migration VTK7
132         #if VTK_MAJOR_VERSION <= 5
133                                  w->SetInput(bbGetInputIn());
134         #else
135                                  w->SetInputData(bbGetInputIn());
136         #endif
137                                  w->SetCompression(bbGetInputCompression());  
138                                  w->SetFileDimensionality(bbGetInputIn()->GetDataDimension());  // NTU
139                                  w->SetFileName(userGivenName.c_str());  
140                                  //w->SetFileName(bbGetInputFilename().c_str());                                // JPR
141                                  w->Write();
142                                  w->Delete();
143                 } // .mhdb
144         } // bbGetInputIn()!=NULL
145
146   
147 }
148 //===== 
149 // 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)
150 //===== 
151 void MetaImageWriter::bbUserSetDefaultValues()
152 {
153
154 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
155 //    Here we initialize the input 'In' to 0
156    bbSetInputCompression(false);
157   
158 }
159 //===== 
160 // 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)
161 //===== 
162 void MetaImageWriter::bbUserInitializeProcessing()
163 {
164
165 //  THE INITIALIZATION METHOD BODY :
166 //    Here does nothing 
167 //    but this is where you should allocate the internal/output pointers 
168 //    if any 
169
170   
171 }
172 //===== 
173 // 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)
174 //===== 
175 void MetaImageWriter::bbUserFinalizeProcessing()
176 {
177
178 //  THE FINALIZATION METHOD BODY :
179 //    Here does nothing 
180 //    but this is where you should desallocate the internal/output pointers 
181 //    if any
182   
183 }
184 }
185 // EO namespace bbvtk
186
187