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