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