]> 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         int slice;
82         int width;
83
84         if (bbGetInputDirectionPlane()=="XY")
85         {
86                 inputfilename=bbGetInputFileName();
87         } // if XY
88
89
90         if ((bbGetInputDirectionPlane()=="YZ") || (bbGetInputDirectionPlane()=="ZX"))
91         {
92
93                 //-- Split FileName
94                 std::size_t found               =       bbGetInputFileName().find_last_of("/\\");
95                 std::string path                =       bbGetInputFileName().substr(0,found+1);
96                 std::string filename    =       bbGetInputFileName().substr(found+1);
97 #ifdef _WIN32
98                 path=path+"YZ_ZX\\";
99 #else
100                 path=path+"YZ_ZX/";
101 #endif
102         
103         
104                 if (bbGetInputDirectionPlane()=="YZ") 
105                 { 
106                         inputfilename = path+filename+"_YZ.mhd";  
107                 } // if YZ
108
109                 if (bbGetInputDirectionPlane()=="ZX")
110                 {
111                         inputfilename = path+filename+"_ZX.mhd";
112                 } // XZ
113         } // if YZ || XZ
114
115         width = bbGetInputWidth();
116         if (width<=0 ) { width=1; }
117
118         slice = bbGetInputSlice();
119         if (slice<0 ) { slice=0; }
120
121
122         vtkImageData *newImage;
123         long long int newHeaderSize;
124     std::string newFileName=inputfilename+"-OneSlice";
125
126     int sx,sy,sz;
127     char mystring[250];
128     char strTmp[20];
129         bool ok=true;
130     FILE *ffIn  = fopen(inputfilename.c_str(),"r");
131         if (ffIn!=NULL)
132         {
133             FILE *ffOut = fopen(newFileName.c_str(),"w");
134
135         while(!feof(ffIn))
136         {
137             strcpy(mystring,"\n");
138                         fgets(mystring,250,ffIn);
139                 if (strncmp("NDims",mystring,5)==0) {
140                                 if (width==1) 
141                                 { 
142                                         strcpy(mystring,"NDims = 2\n"); 
143                                 } else {
144                                         strcpy(mystring,"NDims = 3\n"); 
145                                 }
146                         }
147                 if (strncmp("DimSize",mystring,6)==0) 
148                         {
149                                 sscanf(mystring,"%s %s %d %d %d",strTmp, strTmp, &sx, &sy,&sz);
150
151                                 if (width==1) 
152                                 { 
153                                         sprintf(mystring,"DimSize = %d %d\n",sx,sy);
154                                 } else {
155                                         sprintf(mystring,"DimSize = %d %d %d\n",sx,sy,width);
156                                 }
157
158                         newHeaderSize = (long long int)sx*(long long int)sy*(long long int)slice;
159                                 if (bbGetInputSlice()>=sz) {ok=false;}
160                         } // if
161                 if (strncmp("ElementType = MET_CHAR",mystring,22)==0)   { newHeaderSize=newHeaderSize*1; }
162                 if (strncmp("ElementType = MET_UCHAR",mystring,23)==0)  { newHeaderSize=newHeaderSize*1; }
163                 if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { newHeaderSize=newHeaderSize*2; }
164                 if (strncmp("ElementType = MET_SHORT",mystring,23)==0)  { newHeaderSize=newHeaderSize*2; }
165                 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0)  { newHeaderSize=newHeaderSize*4; }
166                 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0)  { newHeaderSize=newHeaderSize*4; }
167                 if (strncmp("Offset",mystring,6)==0) {strcpy(mystring,"Offset = 0 0 0\n");}
168                 if (strncmp("HeaderSize",mystring,10)==0) {strcpy(mystring,"");}
169                 if (strncmp("ElementDataFile",mystring,15)==0) 
170                         {
171                                 fprintf(ffOut,"HeaderSize = %lld\n\n", newHeaderSize );
172                         } // if
173                 fprintf(ffOut,mystring); 
174         } // while
175                 fclose(ffIn);
176                 fclose(ffOut);
177
178                 if (ok==true)
179                 {
180                         vtkMetaImageReader * reader = vtkMetaImageReader::New();
181                         reader->SetFileName( newFileName.c_str() );
182                         reader->Update();
183                         newImage = reader->GetOutput();
184                 } // if ok
185         } else {
186                 ok=false;
187                 printf("EED ERROR: Problem openin:%s\n", inputfilename.c_str() );
188         }
189
190         if (ok==false)
191         {
192                 newImage=CreateDefaultImage();
193         } // if ok
194
195         bbSetOutputOut( newImage );
196 }
197
198
199
200
201
202 void ReadMHDPlane::Read64lseek()
203 {
204         int             slice;
205         int             width;
206
207         width = bbGetInputWidth();
208         if (width<=0 ) { width=1; }
209
210         slice = bbGetInputSlice();
211         if (slice<0 ) { slice=0; }
212
213         int             dimX=-1,dimY=-1,dimZ=-1;
214         int             dim=-1;
215         std::string formattype("VOID");
216         std::string elementdatafile("VOID");
217         float           spcX=-1,spcY=-1,spcZ=-1;
218         float           ox=-1,oy=-1,oz=-1;
219         long int        headersize=0;
220
221         vtkImageData *newImage=NULL;
222
223         char mystring[250];
224         char strTmp[30];
225         char strTmp2[30];
226         FILE *ffIn      = fopen(bbGetInputFileName().c_str(),"r");
227         long long dataSize;
228
229         if (ffIn!=NULL)
230         {
231                 newImage = vtkImageData::New();
232
233                 while(!feof(ffIn))
234                 {
235             strcpy(mystring,"\n");
236                         fgets(mystring,250,ffIn);
237                 if (strncmp("NDims",mystring,5)==0)                     { sscanf(mystring,"%s %s %d"            ,strTmp, strTmp, &dim);                                 }
238                 if (strncmp("DimSize",mystring,6)==0)                   { sscanf(mystring,"%s %s %d %d %d"      ,strTmp, strTmp, &dimX, &dimY,&dimZ);   } 
239                         if (strncmp("ElementType",mystring,11)==0)              { sscanf(mystring,"%s %s %s"            ,strTmp, strTmp, strTmp2); formattype=strTmp2;                  }
240                         if (strncmp("ElementSpacing",mystring,11)==0)   { sscanf(mystring,"%s %s %f %f %f"      ,strTmp, strTmp, &spcX,&spcY,&spcZ);    }
241                 if (strncmp("Offset",mystring,6)==0)                    { sscanf(mystring,"%s %s %f %f %f"      ,strTmp, strTmp, &ox, &oy, &oz);                }
242                 if (strncmp("HeaderSize",mystring,10)==0)               { sscanf(mystring,"%s %s %ld"           ,strTmp, strTmp, &headersize);                  }
243                         if (strncmp("ElementDataFile",mystring,15)==0)  { sscanf(mystring,"%s %s %s"            ,strTmp, strTmp, strTmp2); elementdatafile=strTmp2;             }
244
245                 if (strncmp("ElementType = MET_CHAR",mystring,22)==0)                   { newImage->SetScalarTypeToChar();                      dataSize=sizeof(char);                  }
246                 if (strncmp("ElementType = VTK_CHAR",mystring,22)==0)                   { newImage->SetScalarTypeToChar();                      dataSize=sizeof(char);                  }
247                 if (strncmp("ElementType = MET_UCHAR",mystring,23)==0)                  { newImage->SetScalarTypeToUnsignedChar();      dataSize=sizeof(unsigned char); }
248                 if (strncmp("ElementType = VTK_UNSIGNED_CHAR",mystring,31)==0)  { newImage->SetScalarTypeToUnsignedChar();      dataSize=sizeof(unsigned char); }
249                 if (strncmp("ElementType = MET_USHORT",mystring,24)==0)                 { newImage->SetScalarTypeToUnsignedShort();     dataSize=sizeof(unsigned short);}
250                 if (strncmp("ElementType = VTK_UNSIGNED_SHORT",mystring,32)==0) { newImage->SetScalarTypeToUnsignedShort();     dataSize=sizeof(unsigned short);}
251                 if (strncmp("ElementType = MET_SHORT",mystring,23)==0)                  { newImage->SetScalarTypeToShort();                     dataSize=sizeof(short);                 }
252                 if (strncmp("ElementType = VTK_SHORT",mystring,23)==0)                  { newImage->SetScalarTypeToShort();                     dataSize=sizeof(short);                 }
253                 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0)                  { newImage->SetScalarTypeToFloat();                     dataSize=sizeof(float);                 }
254                 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0)                  { newImage->SetScalarTypeToFloat();                     dataSize=sizeof(float);                 }
255                 if (strncmp("ElementType = MET_DOUBLE",mystring,24)==0)                 { newImage->SetScalarTypeToDouble();            dataSize=sizeof(double);                }
256                 if (strncmp("ElementType = VTK_DOUBLE",mystring,24)==0)                 { newImage->SetScalarTypeToDouble();            dataSize=sizeof(double);                }
257
258         } // while
259                 fclose(ffIn);
260
261                 newImage->Initialize();
262
263
264                 int             fd;
265                 long long       ret;
266                 std::size_t found;
267                 std::string filename;
268                 found                                   = bbGetInputFileName().find_last_of("/\\");
269                 filename                                = bbGetInputFileName().substr(0,found+1) + elementdatafile ;
270                 long long       pos;
271
272                 long long lsize                 = dimX*dimY*width *dataSize;
273
274
275 #if defined(_WIN32)
276                    _sopen_s( &fd, filename.c_str(), _O_RDONLY, _SH_DENYNO, 0 );
277 #else
278                 fd = open ( filename.c_str() ,  O_RDONLY|O_LARGEFILE );
279 #endif // defined(_WIN32)
280
281
282
283                 if (fd < 0)
284                 {
285                         printf("EED ReadMHDPlane::Read64lseek \n");
286                         fprintf(stderr, "%s\n", strerror(errno));
287                         exit(1);
288                 }
289
290                 if (bbGetInputDirectionPlane()=="XY") 
291                 { 
292                         newImage->SetSpacing( spcX,spcY,spcZ );
293                         newImage->SetDimensions(  dimX,dimY,width );
294                         newImage->SetWholeExtent(0,  dimX-1,0,dimY-1,0,width-1 );
295                         newImage->SetExtent(0,  dimX-1,0,dimY-1,0,width-1 );
296                         newImage->SetNumberOfScalarComponents(1);
297                         newImage->AllocateScalars();
298                         newImage->Update();
299                         pos = dimX*dimY*(long long)slice*dataSize;
300 #if defined(_WIN32)
301                         if (_lseeki64( fd, pos, SEEK_SET ) < 0)
302 #else
303                         if (lseek64(fd, pos, SEEK_SET) < 0) 
304 #endif // defined(_WIN32)
305                         {
306                                 printf("EED ReadMHDPlane::Read64lseek \n");
307                                 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
308                                 exit(1);
309                         }
310
311                         if ((ret = read(fd, newImage->GetScalarPointer() , lsize)) < 0) 
312                         {
313                                 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
314                                 exit(1);
315                         }
316                 }  // if PLANE XY
317
318                 if (bbGetInputDirectionPlane()=="XZ") 
319                 { 
320                         newImage->SetSpacing( spcX,spcZ,spcY );
321                         newImage->SetDimensions(  dimX,dimZ,width );
322                         newImage->SetWholeExtent(0,  dimX-1,0,dimZ-1,0,width-1 );
323                         newImage->SetExtent(0,  dimX-1,0,dimZ-1,0,width-1 );
324                         newImage->SetNumberOfScalarComponents(1);
325                         newImage->AllocateScalars();
326                         newImage->Update();
327                         int iWidth;
328                         for (iWidth=0;iWidth<width;iWidth++)
329                         {
330                                 copy_XZ_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
331                         }
332                 }  // if PLANE XZ
333
334
335                 if (bbGetInputDirectionPlane()=="YZ") 
336                 { 
337                         newImage->SetSpacing( spcY,spcZ,spcX );
338                         newImage->SetDimensions(  dimY,dimZ,width );
339                         newImage->SetWholeExtent(0,  dimY-1,0,dimZ-1,0,width-1 );
340                         newImage->SetExtent(0,  dimY-1,0,dimZ-1,0,width-1 );
341                         newImage->SetNumberOfScalarComponents(1);
342                         newImage->AllocateScalars();
343                         newImage->Update();
344                         int iWidth;
345                         for (iWidth=0;iWidth<width;iWidth++)
346                         {
347                                 copy_YZ_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
348                         }
349
350                 }  // if PLANE YZ       
351 #if defined(_WIN32)
352                 _close (fd);
353 #else
354                 close (fd);
355 #endif // defined(_WIN32)
356         } else {
357                 newImage=CreateDefaultImage();
358         } // if  ffIn
359
360         bbSetOutputOut( newImage );
361 }
362
363
364 void ReadMHDPlane::copy_YZ_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
365 {
366         long long int j;
367         long long int i;
368         long long       ret;
369         long long       pos;
370         long long sizeBytesPlane = dimX*dimY*dataSize;
371         char *pImage;
372         for (j=0;j<dimZ;j++)
373         {
374                 for (i=0;i<dimY;i++)
375                 {
376                         pos = ((long long int)slice+iWidth + i*dimX + j*dimX*dimY)*dataSize ;
377 #if defined(_WIN32)
378                         if (_lseeki64( fd, pos , SEEK_SET ) < 0)
379 #else
380                         if (lseek64(fd, pos , SEEK_SET) < 0) 
381 #endif // defined(_WIN32)
382                         {
383                                 printf("EED ReadMHDPlane::Read64lseek \n");
384                                 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
385                                 exit(1);
386                         }
387
388                         pImage=(char*)(newImage->GetScalarPointer(i, dimZ-1-j,iWidth ));
389                         if ((ret = read(fd, pImage , dataSize)) < 0) 
390                         {
391                                 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
392                                 exit(1);
393                         }
394                         } // for i
395                 } // for j
396 }
397
398
399 void ReadMHDPlane::copy_XZ_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
400 {
401         long long int j;
402         long long       ret;
403         long long       pos;
404         pos = dimX*(long long int)slice*dataSize;
405         long long sizeBytesPlane = dimX*dimY*dataSize;
406         char *pImage = (char*)( newImage->GetScalarPointer(0,0,iWidth) );
407         for (j=dimZ-1;j>=0;j--)
408         {
409 #if defined(_WIN32)
410                 if (_lseeki64( fd, pos + j*sizeBytesPlane , SEEK_SET ) < 0)
411 #else
412                 if (lseek64(fd, pos + j*sizeBytesPlane , SEEK_SET) < 0) 
413 #endif // defined(_WIN32)
414                 {
415                         printf("EED ReadMHDPlane::Read64lseek \n");
416                         fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
417                         exit(1);
418                 }
419
420                 if ((ret = read(fd, pImage , dimX*dataSize)) < 0) 
421                 {
422                         fprintf(stderr, "Failed reading: %s\n", strerror(errno));
423                         exit(1);
424                 }
425                 pImage = pImage+dimX*dataSize;
426         } // for j
427 }
428
429
430
431 void ReadMHDPlane::Process()
432 {
433         if (bbGetInputType()==0)
434         {
435                 ReadNormalMHD();
436         }       
437
438         if (bbGetInputType()==1)
439         {
440                 Read64lseek();
441         }       
442
443 }
444 //===== 
445 // 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)
446 //===== 
447 void ReadMHDPlane::bbUserSetDefaultValues()
448 {
449
450 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
451 //    Here we initialize the input 'In' to 0
452    bbSetInputFileName("");
453    bbSetInputSlice(0);
454    bbSetInputType(1);
455    bbSetInputWidth(1);
456    bbSetInputDirectionPlane("XY");
457   
458 }
459 //===== 
460 // 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)
461 //===== 
462 void ReadMHDPlane::bbUserInitializeProcessing()
463 {
464
465 //  THE INITIALIZATION METHOD BODY :
466 //    Here does nothing 
467 //    but this is where you should allocate the internal/output pointers 
468 //    if any 
469
470   
471 }
472 //===== 
473 // 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)
474 //===== 
475 void ReadMHDPlane::bbUserFinalizeProcessing()
476 {
477
478 //  THE FINALIZATION METHOD BODY :
479 //    Here does nothing 
480 //    but this is where you should desallocate the internal/output pointers 
481 //    if any
482   
483 }
484 }
485 // EO namespace bbcreaVtk
486
487