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