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