]> Creatis software - creaVtk.git/blob - bbtk_creaVtk_PKG/src/bbcreaVtkReadMHDPlane.cxx
42f6292a7d8b41b46e7b2dfa2ae2711a11bc80e5
[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
36
37         vtkImageData *newImage;
38         long int newHeaderSize;
39     std::string newFileName=bbGetInputFileName()+"-OneSlice";
40
41     int sx,sy,sz;
42     char mystring[250];
43     char strTmp[20];
44         bool ok=true;
45     FILE *ffIn  = fopen(bbGetInputFileName().c_str(),"r+");
46         if (ffIn!=NULL){
47             FILE *ffOut = fopen(newFileName.c_str(),"w+");
48
49         while(!feof(ffIn))
50         {
51             strcpy(mystring,"\n");
52                         fgets(mystring,250,ffIn);
53                 if (strncmp("NDims",mystring,5)==0) {strcpy(mystring,"NDims = 2\n");}
54                 if (strncmp("DimSize",mystring,6)==0) 
55                         {
56                                 sscanf(mystring,"%s %s %d %d %d",strTmp, strTmp, &sx, &sy,&sz);
57                                 sprintf(mystring,"DimSize = %d %d\n",sx,sy);
58                         newHeaderSize = sx*sy*bbGetInputSlice();
59                                 if (bbGetInputSlice()>=sz) {ok=false;}
60                         } // if
61                 if (strncmp("ElementType = MET_CHAR",mystring,24)==0)   { newHeaderSize=newHeaderSize*1; }
62                 if (strncmp("ElementType = MET_UCHAR",mystring,24)==0)  { newHeaderSize=newHeaderSize*1; }
63                 if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { newHeaderSize=newHeaderSize*2; }
64                 if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { newHeaderSize=newHeaderSize*2; }
65                 if (strncmp("ElementType = MET_SHORT",mystring,23)==0)  { newHeaderSize=newHeaderSize*2; }
66                 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0)  { newHeaderSize=newHeaderSize*4; }
67                 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0)  { newHeaderSize=newHeaderSize*4; }
68                 if (strncmp("Offset",mystring,6)==0) {strcpy(mystring,"Offset = 0 0 0\n");}
69                 if (strncmp("HeaderSize",mystring,10)==0) {strcpy(mystring,"");}
70                 if (strncmp("ElementDataFile",mystring,15)==0) 
71                         {
72                                 fprintf(ffOut,"HeaderSize = %ld\n\n", newHeaderSize );
73                         } // if
74                 fprintf(ffOut,mystring); 
75         } // while
76                 fclose(ffIn);
77                 fclose(ffOut);
78
79                 if (ok==true)
80                 {
81                         vtkMetaImageReader * reader = vtkMetaImageReader::New();
82                         reader->SetFileName( newFileName.c_str() );
83                         reader->Update();
84                         newImage = reader->GetOutput();
85                 } // if ok
86         } else {
87                 ok=false;
88         }
89
90         if (ok==false)
91         {
92                 int i,j;
93                 int sizeX, sizeY, sizeZ;
94                 sizeX = 200;
95                 sizeY = sizeX;
96                 sizeZ = 1;
97                 newImage = vtkImageData::New();
98                 newImage->Initialize();
99                 newImage->SetScalarTypeToUnsignedChar();
100                 newImage->SetSpacing( 1,1,1 );
101                 newImage->SetDimensions(  sizeX,sizeY,sizeZ );
102                 newImage->SetWholeExtent(0,  sizeX-1,0,sizeY-1,0,sizeZ-1 );
103                 newImage->SetExtent(0,  sizeX-1,0,sizeY-1,0,sizeZ-1 );
104                 newImage->SetNumberOfScalarComponents(1);
105                 newImage->AllocateScalars();
106                 newImage->Update();
107                 memset ( (void*)newImage->GetScalarPointer(), 0, sizeX*sizeY*1 );
108                 for (i=0; i<sizeX; i++)
109                 {
110                         newImage->SetScalarComponentFromDouble(i,i,0, 0, 255 );
111                         newImage->SetScalarComponentFromDouble(i,sizeY-1-i,0, 0, 255 );
112                 } // for i
113         } // if ok
114
115         bbSetOutputOut( newImage );
116
117 }
118 //===== 
119 // 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)
120 //===== 
121 void ReadMHDPlane::bbUserSetDefaultValues()
122 {
123
124 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
125 //    Here we initialize the input 'In' to 0
126    bbSetInputFileName("");
127    bbSetInputSlice(0);
128   
129 }
130 //===== 
131 // 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)
132 //===== 
133 void ReadMHDPlane::bbUserInitializeProcessing()
134 {
135
136 //  THE INITIALIZATION METHOD BODY :
137 //    Here does nothing 
138 //    but this is where you should allocate the internal/output pointers 
139 //    if any 
140
141   
142 }
143 //===== 
144 // 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)
145 //===== 
146 void ReadMHDPlane::bbUserFinalizeProcessing()
147 {
148
149 //  THE FINALIZATION METHOD BODY :
150 //    Here does nothing 
151 //    but this is where you should desallocate the internal/output pointers 
152 //    if any
153   
154 }
155 }
156 // EO namespace bbcreaVtk
157
158