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