]> Creatis software - creaVtk.git/blob - bbtk_creaVtk_PKG/src/bbcreaVtkReadMHDPlane.cxx
#2913 creaVtk Feature New Normal - new box MHDFileInfo
[creaVtk.git] / bbtk_creaVtk_PKG / src / bbcreaVtkReadMHDPlane.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 "bbcreaVtkReadMHDPlane.h"
5 #include "bbcreaVtkPackage.h"
6
7 #include "stdio.h"
8
9 namespace bbcreaVtk
10 {
11
12 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,ReadMHDPlane)
13 BBTK_BLACK_BOX_IMPLEMENTATION(ReadMHDPlane,bbtk::AtomicBlackBox);
14 //===== 
15 // 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)
16 //===== 
17 void ReadMHDPlane::Process()
18 {
19
20 // THE MAIN PROCESSING METHOD BODY
21 //   Here we simply set the input 'In' value to the output 'Out'
22 //   And print out the output value
23 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
24 //    void bbSet{Input|Output}NAME(const TYPE&)
25 //    const TYPE& bbGet{Input|Output}NAME() const 
26 //    Where :
27 //    * NAME is the name of the input/output
28 //      (the one provided in the attribute 'name' of the tag 'input')
29 //    * TYPE is the C++ type of the input/output
30 //      (the one provided in the attribute 'type' of the tag 'input')
31
32 //    bbSetOutputOut( bbGetInputIn() );
33 //    std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
34
35         std::string inputfilename;
36
37         if (bbGetInputDirectionPlane()=="XY")
38         {
39                 inputfilename=bbGetInputFileName();
40         } // if XY
41
42
43         if ((bbGetInputDirectionPlane()=="YZ") || (bbGetInputDirectionPlane()=="ZX"))
44         {
45
46                 //-- Split FileName
47                 std::size_t found               =       bbGetInputFileName().find_last_of("/\\");
48                 std::string path                =       bbGetInputFileName().substr(0,found+1);
49                 std::string filename    =       bbGetInputFileName().substr(found+1);
50 #ifdef _WIN32
51                 path=path+"YZ_ZX\\";
52 #else
53                 path=path+"YZ_ZX/";
54 #endif
55         
56         
57                 if (bbGetInputDirectionPlane()=="YZ") 
58                 { 
59                         inputfilename = path+filename+"_YZ.mhd";  
60                 } // if YZ
61
62                 if (bbGetInputDirectionPlane()=="ZX")
63                 {
64                         inputfilename = path+filename+"_ZX.mhd";
65                 } // XZ
66         } // if YZ || XZ
67
68         vtkImageData *newImage;
69         long int newHeaderSize;
70     std::string newFileName=inputfilename+"-OneSlice";
71
72     int sx,sy,sz;
73     char mystring[250];
74     char strTmp[20];
75         bool ok=true;
76     FILE *ffIn  = fopen(inputfilename.c_str(),"r");
77         if (ffIn!=NULL)
78         {
79             FILE *ffOut = fopen(newFileName.c_str(),"w");
80
81         while(!feof(ffIn))
82         {
83             strcpy(mystring,"\n");
84                         fgets(mystring,250,ffIn);
85                 if (strncmp("NDims",mystring,5)==0) {strcpy(mystring,"NDims = 2\n");}
86                 if (strncmp("DimSize",mystring,6)==0) 
87                         {
88                                 sscanf(mystring,"%s %s %d %d %d",strTmp, strTmp, &sx, &sy,&sz);
89                                 sprintf(mystring,"DimSize = %d %d\n",sx,sy);
90                         newHeaderSize = sx*sy*bbGetInputSlice();
91                                 if (bbGetInputSlice()>=sz) {ok=false;}
92                         } // if
93                 if (strncmp("ElementType = MET_CHAR",mystring,22)==0)   { newHeaderSize=newHeaderSize*1; }
94                 if (strncmp("ElementType = MET_UCHAR",mystring,23)==0)  { newHeaderSize=newHeaderSize*1; }
95                 if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { newHeaderSize=newHeaderSize*2; }
96                 if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { newHeaderSize=newHeaderSize*2; }
97                 if (strncmp("ElementType = MET_SHORT",mystring,23)==0)  { newHeaderSize=newHeaderSize*2; }
98                 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0)  { newHeaderSize=newHeaderSize*4; }
99                 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0)  { newHeaderSize=newHeaderSize*4; }
100                 if (strncmp("Offset",mystring,6)==0) {strcpy(mystring,"Offset = 0 0 0\n");}
101                 if (strncmp("HeaderSize",mystring,10)==0) {strcpy(mystring,"");}
102                 if (strncmp("ElementDataFile",mystring,15)==0) 
103                         {
104                                 fprintf(ffOut,"HeaderSize = %ld\n\n", newHeaderSize );
105                         } // if
106                 fprintf(ffOut,mystring); 
107         } // while
108                 fclose(ffIn);
109                 fclose(ffOut);
110
111                 if (ok==true)
112                 {
113                         vtkMetaImageReader * reader = vtkMetaImageReader::New();
114                         reader->SetFileName( newFileName.c_str() );
115                         reader->Update();
116                         newImage = reader->GetOutput();
117                 } // if ok
118         } else {
119                 ok=false;
120                 printf("EED ERROR: Problem openin:%s\n", inputfilename.c_str() );
121         }
122
123         if (ok==false)
124         {
125                 int i;
126                 int sizeX, sizeY, sizeZ;
127                 sizeX = 200;
128                 sizeY = sizeX;
129                 sizeZ = 1;
130                 newImage = vtkImageData::New();
131                 newImage->Initialize();
132                 newImage->SetScalarTypeToUnsignedChar();
133                 newImage->SetSpacing( 1,1,1 );
134                 newImage->SetDimensions(  sizeX,sizeY,sizeZ );
135                 newImage->SetWholeExtent(0,  sizeX-1,0,sizeY-1,0,sizeZ-1 );
136                 newImage->SetExtent(0,  sizeX-1,0,sizeY-1,0,sizeZ-1 );
137                 newImage->SetNumberOfScalarComponents(1);
138                 newImage->AllocateScalars();
139                 newImage->Update();
140                 memset ( (void*)newImage->GetScalarPointer(), 0, sizeX*sizeY*1 );
141                 for (i=0; i<sizeX; i++)
142                 {
143                         newImage->SetScalarComponentFromDouble(i,i,0, 0, 255 );
144                         newImage->SetScalarComponentFromDouble(i,sizeY-1-i,0, 0, 255 );
145                 } // for i
146         } // if ok
147
148         bbSetOutputOut( newImage );
149
150 }
151 //===== 
152 // 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)
153 //===== 
154 void ReadMHDPlane::bbUserSetDefaultValues()
155 {
156
157 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
158 //    Here we initialize the input 'In' to 0
159    bbSetInputFileName("");
160    bbSetInputSlice(0);
161    bbSetInputDirectionPlane("XY");
162   
163 }
164 //===== 
165 // 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)
166 //===== 
167 void ReadMHDPlane::bbUserInitializeProcessing()
168 {
169
170 //  THE INITIALIZATION METHOD BODY :
171 //    Here does nothing 
172 //    but this is where you should allocate the internal/output pointers 
173 //    if any 
174
175   
176 }
177 //===== 
178 // 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)
179 //===== 
180 void ReadMHDPlane::bbUserFinalizeProcessing()
181 {
182
183 //  THE FINALIZATION METHOD BODY :
184 //    Here does nothing 
185 //    but this is where you should desallocate the internal/output pointers 
186 //    if any
187   
188 }
189 }
190 // EO namespace bbcreaVtk
191
192