]> Creatis software - creaVtk.git/blob - bbtk_creaVtk_PKG/src/bbcreaVtkReadMHDPlane.cxx
#2711 creaVtk Feature New Normal - new Box ReadMHDPlane
[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 #include <sys/types.h>
10 #include <unistd.h> 
11
12
13
14
15 #define _LARGEFILE64_SOURCE
16
17 #include <stdio.h>
18 #include <sys/types.h>
19 #include <unistd.h>
20 #include <stdlib.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25
26 namespace bbcreaVtk
27 {
28
29 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,ReadMHDPlane)
30 BBTK_BLACK_BOX_IMPLEMENTATION(ReadMHDPlane,bbtk::AtomicBlackBox);
31 //===== 
32 // 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)
33 //===== 
34
35 void ReadMHDPlane::ReadNormalMHD()
36 {
37 // THE MAIN PROCESSING METHOD BODY
38 //   Here we simply set the input 'In' value to the output 'Out'
39 //   And print out the output value
40 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
41 //    void bbSet{Input|Output}NAME(const TYPE&)
42 //    const TYPE& bbGet{Input|Output}NAME() const 
43 //    Where :
44 //    * NAME is the name of the input/output
45 //      (the one provided in the attribute 'name' of the tag 'input')
46 //    * TYPE is the C++ type of the input/output
47 //      (the one provided in the attribute 'type' of the tag 'input')
48
49 //    bbSetOutputOut( bbGetInputIn() );
50 //    std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
51
52         std::string inputfilename;
53
54         if (bbGetInputDirectionPlane()=="XY")
55         {
56                 inputfilename=bbGetInputFileName();
57         } // if XY
58
59
60         if ((bbGetInputDirectionPlane()=="YZ") || (bbGetInputDirectionPlane()=="ZX"))
61         {
62
63                 //-- Split FileName
64                 std::size_t found               =       bbGetInputFileName().find_last_of("/\\");
65                 std::string path                =       bbGetInputFileName().substr(0,found+1);
66                 std::string filename    =       bbGetInputFileName().substr(found+1);
67 #ifdef _WIN32
68                 path=path+"YZ_ZX\\";
69 #else
70                 path=path+"YZ_ZX/";
71 #endif
72         
73         
74                 if (bbGetInputDirectionPlane()=="YZ") 
75                 { 
76                         inputfilename = path+filename+"_YZ.mhd";  
77                 } // if YZ
78
79                 if (bbGetInputDirectionPlane()=="ZX")
80                 {
81                         inputfilename = path+filename+"_ZX.mhd";
82                 } // XZ
83         } // if YZ || XZ
84
85         vtkImageData *newImage;
86         long long int newHeaderSize;
87     std::string newFileName=inputfilename+"-OneSlice";
88
89     int sx,sy,sz;
90     char mystring[250];
91     char strTmp[20];
92         bool ok=true;
93     FILE *ffIn  = fopen(inputfilename.c_str(),"r");
94         if (ffIn!=NULL)
95         {
96             FILE *ffOut = fopen(newFileName.c_str(),"w");
97
98         while(!feof(ffIn))
99         {
100             strcpy(mystring,"\n");
101                         fgets(mystring,250,ffIn);
102                 if (strncmp("NDims",mystring,5)==0) {strcpy(mystring,"NDims = 2\n");}
103                 if (strncmp("DimSize",mystring,6)==0) 
104                         {
105                                 sscanf(mystring,"%s %s %d %d %d",strTmp, strTmp, &sx, &sy,&sz);
106                                 sprintf(mystring,"DimSize = %d %d\n",sx,sy);
107                         newHeaderSize = (long long int)sx*(long long int)sy*(long long int)bbGetInputSlice();
108                                 if (bbGetInputSlice()>=sz) {ok=false;}
109                         } // if
110                 if (strncmp("ElementType = MET_CHAR",mystring,22)==0)   { newHeaderSize=newHeaderSize*1; }
111                 if (strncmp("ElementType = MET_UCHAR",mystring,23)==0)  { newHeaderSize=newHeaderSize*1; }
112                 if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { newHeaderSize=newHeaderSize*2; }
113                 if (strncmp("ElementType = MET_SHORT",mystring,23)==0)  { newHeaderSize=newHeaderSize*2; }
114                 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0)  { newHeaderSize=newHeaderSize*4; }
115                 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0)  { newHeaderSize=newHeaderSize*4; }
116                 if (strncmp("Offset",mystring,6)==0) {strcpy(mystring,"Offset = 0 0 0\n");}
117                 if (strncmp("HeaderSize",mystring,10)==0) {strcpy(mystring,"");}
118                 if (strncmp("ElementDataFile",mystring,15)==0) 
119                         {
120                                 fprintf(ffOut,"HeaderSize = %lld\n\n", newHeaderSize );
121                         } // if
122                 fprintf(ffOut,mystring); 
123         } // while
124                 fclose(ffIn);
125                 fclose(ffOut);
126
127                 if (ok==true)
128                 {
129                         vtkMetaImageReader * reader = vtkMetaImageReader::New();
130                         reader->SetFileName( newFileName.c_str() );
131                         reader->Update();
132                         newImage = reader->GetOutput();
133                 } // if ok
134         } else {
135                 ok=false;
136                 printf("EED ERROR: Problem openin:%s\n", inputfilename.c_str() );
137         }
138
139         if (ok==false)
140         {
141                 int i;
142                 int sizeX, sizeY, sizeZ;
143                 sizeX = 200;
144                 sizeY = sizeX;
145                 sizeZ = 1;
146                 newImage = vtkImageData::New();
147                 newImage->Initialize();
148                 newImage->SetScalarTypeToUnsignedChar();
149                 newImage->SetSpacing( 1,1,1 );
150                 newImage->SetDimensions(  sizeX,sizeY,sizeZ );
151                 newImage->SetWholeExtent(0,  sizeX-1,0,sizeY-1,0,sizeZ-1 );
152                 newImage->SetExtent(0,  sizeX-1,0,sizeY-1,0,sizeZ-1 );
153                 newImage->SetNumberOfScalarComponents(1);
154                 newImage->AllocateScalars();
155                 newImage->Update();
156                 memset ( (void*)newImage->GetScalarPointer(), 0, sizeX*sizeY*1 );
157                 for (i=0; i<sizeX; i++)
158                 {
159                         newImage->SetScalarComponentFromDouble(i,i,0, 0, 255 );
160                         newImage->SetScalarComponentFromDouble(i,sizeY-1-i,0, 0, 255 );
161                 } // for i
162         } // if ok
163
164         bbSetOutputOut( newImage );
165
166 }
167
168 void ReadMHDPlane::Read64lseek()
169 {
170
171         int             dimX=-1,dimY=-1,dimZ=-1;
172         int             dim=-1;
173         std::string formattype("VOID");
174         std::string elementdatafile("VOID");
175         float           spcX=-1,spcY=-1,spcZ=-1;
176         float           ox=-1,oy=-1,oz=-1;
177         long int        headersize=0;
178
179         vtkImageData *newImage;
180
181         char mystring[250];
182         char strTmp[30];
183         char strTmp2[30];
184         FILE *ffIn      = fopen(bbGetInputFileName().c_str(),"r");
185         long long dataSize;
186
187
188
189         if (ffIn!=NULL)
190         {
191                 newImage = vtkImageData::New();
192
193                 while(!feof(ffIn))
194                 {
195             strcpy(mystring,"\n");
196                         fgets(mystring,250,ffIn);
197                 if (strncmp("NDims",mystring,5)==0)                     { sscanf(mystring,"%s %s %d"            ,strTmp, strTmp, &dim);                                 }
198                 if (strncmp("DimSize",mystring,6)==0)                   { sscanf(mystring,"%s %s %d %d %d"      ,strTmp, strTmp, &dimX, &dimY,&dimZ);   } 
199                         if (strncmp("ElementType",mystring,11)==0)              { sscanf(mystring,"%s %s %s"            ,strTmp, strTmp, strTmp2); formattype=strTmp2;                  }
200                         if (strncmp("ElementSpacing",mystring,11)==0)   { sscanf(mystring,"%s %s %f %f %f"      ,strTmp, strTmp, &spcX,&spcY,&spcZ);    }
201                 if (strncmp("Offset",mystring,6)==0)                    { sscanf(mystring,"%s %s %f %f %f"      ,strTmp, strTmp, &ox, &oy, &oz);                }
202                 if (strncmp("HeaderSize",mystring,10)==0)               { sscanf(mystring,"%s %s %ld"           ,strTmp, strTmp, &headersize);                  }
203                         if (strncmp("ElementDataFile",mystring,15)==0)  { sscanf(mystring,"%s %s %s"            ,strTmp, strTmp, strTmp2); elementdatafile=strTmp2;             }
204
205                 if (strncmp("ElementType = MET_CHAR",mystring,22)==0)   { newImage->SetScalarTypeToChar();                      dataSize=sizeof(char);                  }
206                 if (strncmp("ElementType = VTK_CHAR",mystring,22)==0)   { newImage->SetScalarTypeToChar();                      dataSize=sizeof(char);                  }
207                 if (strncmp("ElementType = MET_UCHAR",mystring,23)==0)  { newImage->SetScalarTypeToUnsignedChar();      dataSize=sizeof(unsigned char); }
208                 if (strncmp("ElementType = VTK_UNSIGNED_CHAR",mystring,31)==0)  { newImage->SetScalarTypeToUnsignedChar();      dataSize=sizeof(unsigned char); }
209                 if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { newImage->SetScalarTypeToUnsignedShort();     dataSize=sizeof(unsigned short);}
210                 if (strncmp("ElementType = VTK_UNSIGNED_SHORT",mystring,32)==0) { newImage->SetScalarTypeToUnsignedShort();     dataSize=sizeof(unsigned short);}
211                 if (strncmp("ElementType = MET_SHORT",mystring,23)==0)  { newImage->SetScalarTypeToShort();                     dataSize=sizeof(short);                 }
212                 if (strncmp("ElementType = VTK_SHORT",mystring,23)==0)  { newImage->SetScalarTypeToShort();                     dataSize=sizeof(short);                 }
213                 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0)  { newImage->SetScalarTypeToFloat();                     dataSize=sizeof(float);                 }
214                 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0)  { newImage->SetScalarTypeToFloat();                     dataSize=sizeof(float);                 }
215                 if (strncmp("ElementType = MET_DOUBLE",mystring,24)==0) { newImage->SetScalarTypeToDouble();            dataSize=sizeof(double);                }
216                 if (strncmp("ElementType = VTK_DOUBLE",mystring,24)==0) { newImage->SetScalarTypeToDouble();            dataSize=sizeof(double);                }
217
218         } // while
219                 fclose(ffIn);
220
221                 newImage->Initialize();
222
223
224                 int fd;
225                 long long ret;
226                 std::size_t found;
227                 std::string filename;
228                 found                                   =       bbGetInputFileName().find_last_of("/\\");
229                 filename                                = bbGetInputFileName().substr(0,found+1) + elementdatafile ;
230                 long long pos;
231
232                 long long lsize                 = dimX*dimY*1 *dataSize;
233                 fd = open ( filename.c_str() ,  O_RDONLY|O_LARGEFILE );
234                 if (fd < 0)
235                 {
236                         printf("EED ReadMHDPlane::Read64lseek \n");
237                         fprintf(stderr, "%s\n", strerror(errno));
238                         exit(1);
239                 }
240
241                 if (bbGetInputDirectionPlane()=="XY") 
242                 { 
243                         newImage->SetSpacing( spcX,spcY,spcZ );
244                         newImage->SetDimensions(  dimX,dimY,1 );
245                         newImage->SetWholeExtent(0,  dimX-1,0,dimY-1,0,0 );
246                         newImage->SetExtent(0,  dimX-1,0,dimY-1,0,0 );
247                         newImage->SetNumberOfScalarComponents(1);
248                         newImage->AllocateScalars();
249                         newImage->Update();
250                         pos = dimX*dimY*(long long)bbGetInputSlice()*dataSize;
251                         if (lseek64(fd, pos, SEEK_SET) < 0) 
252                         {
253                                 printf("EED ReadMHDPlane::Read64lseek \n");
254                                 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
255                                 exit(1);
256                         }
257
258                         if ((ret = read(fd, newImage->GetScalarPointer() , lsize)) < 0) 
259                         {
260                                 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
261                                 exit(1);
262                         }
263                 }  // if PLANE XY
264
265
266                 if (bbGetInputDirectionPlane()=="XZ") 
267                 { 
268                         long long int j;
269                         newImage->SetSpacing( spcX,spcZ,spcY );
270                         newImage->SetDimensions(  dimX,dimZ,1 );
271                         newImage->SetWholeExtent(0,  dimX-1,0,dimZ-1,0,0 );
272                         newImage->SetExtent(0,  dimX-1,0,dimZ-1,0,0 );
273                         newImage->SetNumberOfScalarComponents(1);
274                         newImage->AllocateScalars();
275                         newImage->Update();
276                         long long sizeBytesPlane = dimX*dimY*dataSize;
277                         pos = dimX*(long long int)bbGetInputSlice()*dataSize;
278                         void *pImage = newImage->GetScalarPointer();
279                         for (j=0;j<dimZ;j++)
280                         {
281 printf("EED EED ReadMHDPlane::Read64lseek XZ j=%d \n", j);
282                                 if (lseek64(fd, pos + j*sizeBytesPlane , SEEK_SET) < 0) 
283                                 {
284                                         printf("EED ReadMHDPlane::Read64lseek \n");
285                                         fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
286                                         exit(1);
287                                 }
288
289                                 if ((ret = read(fd, pImage , dimX*dataSize)) < 0) 
290                                 {
291                                         fprintf(stderr, "Failed reading: %s\n", strerror(errno));
292                                         exit(1);
293                                 }
294                                 pImage=pImage+dimX*dataSize;
295                         } // for j
296                 }  // if PLANE XY
297
298
299                 if (bbGetInputDirectionPlane()=="YZ") 
300                 { 
301                         long long int j;
302                         long long int i;
303                         newImage->SetSpacing( spcY,spcZ,spcX );
304                         newImage->SetDimensions(  dimY,dimZ,1 );
305                         newImage->SetWholeExtent(0,  dimY-1,0,dimZ-1,0,0 );
306                         newImage->SetExtent(0,  dimY-1,0,dimZ-1,0,0 );
307                         newImage->SetNumberOfScalarComponents(1);
308                         newImage->AllocateScalars();
309                         newImage->Update();
310                         long long sizeBytesPlane = dimX*dimY*dataSize;
311 //                      pos = dimX*(long long int)bbGetInputSlice()*dataSize;
312                         void *pImage;
313                         for (j=0;j<dimZ;j++)
314                         {
315 printf("EED EED ReadMHDPlane::Read64lseek YZ i=%d j=%d \n", i,j);
316                                 for (i=0;i<dimY;i++)
317                                 {
318                                         pos = ((long long int)bbGetInputSlice() + i*dimX + j*dimX*dimY)*dataSize ;
319                                         if (lseek64(fd, pos  , SEEK_SET) < 0) 
320                                         {
321                                                 printf("EED ReadMHDPlane::Read64lseek \n");
322                                                 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
323                                                 exit(1);
324                                         }
325
326                                         pImage=newImage->GetScalarPointer(i, j,0 );
327                                         if ((ret = read(fd, pImage , dataSize)) < 0) 
328                                         {
329                                                 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
330                                                 exit(1);
331                                         }
332                                 }
333                         } // for j
334                 }  // if PLANE XY
335
336         
337                 close (fd);
338
339                 bbSetOutputOut( newImage );
340         } // if
341
342
343 }
344
345
346 void ReadMHDPlane::Process()
347 {
348         if (bbGetInputType()==0)
349         {
350                 ReadNormalMHD();
351         }       
352
353         if (bbGetInputType()==1)
354         {
355                 Read64lseek();
356         }       
357
358 }
359 //===== 
360 // 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)
361 //===== 
362 void ReadMHDPlane::bbUserSetDefaultValues()
363 {
364
365 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
366 //    Here we initialize the input 'In' to 0
367    bbSetInputFileName("");
368    bbSetInputSlice(0);
369    bbSetInputType(0);
370    bbSetInputDirectionPlane("XY");
371   
372 }
373 //===== 
374 // 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)
375 //===== 
376 void ReadMHDPlane::bbUserInitializeProcessing()
377 {
378
379 //  THE INITIALIZATION METHOD BODY :
380 //    Here does nothing 
381 //    but this is where you should allocate the internal/output pointers 
382 //    if any 
383
384   
385 }
386 //===== 
387 // 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)
388 //===== 
389 void ReadMHDPlane::bbUserFinalizeProcessing()
390 {
391
392 //  THE FINALIZATION METHOD BODY :
393 //    Here does nothing 
394 //    but this is where you should desallocate the internal/output pointers 
395 //    if any
396   
397 }
398 }
399 // EO namespace bbcreaVtk
400
401