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