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)
4 #include "bbvtkMetaImageWriter.h"
5 #include "bbvtkPackage.h"
7 #include <vtkExtractVOI.h>
8 #include <vtkMetaImageWriter.h>
10 #include "vtkJSONImageWriter.h"
11 #include "vtkXMLImageDataWriter.h"
12 #include "vtkXMLDataSetWriter.h"
13 #include "vtkDataSetWriter.h"
14 #include "vtkPDataSetWriter.h"
15 #include "vtkXMLWriter.h"
20 BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,MetaImageWriter)
21 BBTK_BLACK_BOX_IMPLEMENTATION(MetaImageWriter,bbtk::AtomicBlackBox);
25 void MetaImageWriter::Save_mhdb( std::string filename , vtkImageData* img ,int sizeB)
30 int elementType=img->GetScalarType();
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);
40 std::string filenameBlock;
41 std::string filenameBlockVti;
42 if (filename.substr(filename.size()-5) == ".mhdb")
45 // cmd="rm -rf "+filename;
46 system( cmd.c_str() );
47 cmd="mkdir "+filename;
48 system( cmd.c_str() );
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 );
65 voi[1]=voi[0]+(sizeB-1);
67 voi[3]=voi[2]+(sizeB-1);
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 );
76 extract->UpdateWholeExtent();
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() );
89 filenameBlockVti=filenameBlock+"-ZLib.vti";
90 vtkXMLImageDataWriter *writer = vtkXMLImageDataWriter::New();
91 writer->SetDataModeToBinary();
92 // writer->SetCompressionLevel(5);
93 writer->SetCompressorTypeToZLib();
94 writer->SetFileName( filenameBlockVti.c_str() );
95 writer->SetInputData( bbGetInputIn() );
108 // 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 void MetaImageWriter::Process()
113 printf("EED vtkMetaImageWriter::Process START\n");
115 // THE MAIN PROCESSING METHOD BODY
116 // Here we simply set the input 'In' value to the output 'Out'
117 // And print out the output value
118 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
119 // void bbSet{Input|Output}NAME(const TYPE&)
120 // const TYPE& bbGet{Input|Output}NAME() const
122 // * NAME is the name of the input/output
123 // (the one provided in the attribute 'name' of the tag 'input')
124 // * TYPE is the C++ type of the input/output
125 // (the one provided in the attribute 'type' of the tag 'input')
127 //EED 2017-01-01 Migration VTK7
128 #if VTK_MAJOR_VERSION <= 5
129 bbGetInputIn()->Update();
133 std::string userGivenName = bbGetInputFilename();
134 if (bbGetInputIn()!=NULL)
136 if (userGivenName.substr(userGivenName.size()-5) == ".mhdb")
138 Save_mhdb( userGivenName , bbGetInputIn() , bbGetInputSizeBlock() );
140 if (!((userGivenName.substr(userGivenName.size()-4) == ".mhd") || (userGivenName.substr(userGivenName.size()-4) == ".mha"))) //JPR
142 userGivenName += ".mhd";
144 vtkMetaImageWriter* w = vtkMetaImageWriter::New();
145 //EED 2017-01-01 Migration VTK7
146 #if VTK_MAJOR_VERSION <= 5
147 w->SetInput(bbGetInputIn());
149 w->SetInputData(bbGetInputIn());
151 w->SetCompression(bbGetInputCompression());
152 w->SetFileDimensionality(bbGetInputIn()->GetDataDimension()); // NTU
153 w->SetFileName(userGivenName.c_str());
154 //w->SetFileName(bbGetInputFilename().c_str()); // JPR
158 } // bbGetInputIn()!=NULL
159 printf("EED vtkMetaImageWriter::Process END\n");
162 // 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)
164 void MetaImageWriter::bbUserSetDefaultValues()
167 // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
168 // Here we initialize the input 'In' to 0
169 bbSetInputCompression(false);
170 bbSetInputSizeBlock(20);
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)
175 void MetaImageWriter::bbUserInitializeProcessing()
178 // THE INITIALIZATION METHOD BODY :
180 // but this is where you should allocate the internal/output pointers
186 // 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)
188 void MetaImageWriter::bbUserFinalizeProcessing()
191 // THE FINALIZATION METHOD BODY :
193 // but this is where you should desallocate the internal/output pointers
198 // EO namespace bbvtk