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